实现一个处理字符串的类String。它用一个动态的字符数组保存一个字符串。实现的功能有:字符串连接(+和+=),字符串赋值(=),字符串的比较(>, >=, < , <=, !=, = =),取字符串的一个子串,访问字符串中的某一个字符,字符串的输入输出(>>和<<)。
//String.h #pragma once #define _CRT_SECURE_NO_WARNINGS # includeusing namespace std; class String { friend String operator+(const String& s1, const String& s2); friend ostream& operator<<(ostream& os, const String& obj); friend istream& operator>>(istream& is, String& obj); friend bool operator>(const String& s1, const String& s2); friend bool operator>=(const String& s1, const String& s2); friend bool operator==(const String& s1, const String& s2); friend bool operator!=(const String& s1, const String& s2); friend bool operator<(const String& s1, const String& s2); friend bool operator<=(const String& s1, const String& s2); private: int len; char* data; public: String(const char* s = ""); String(const String& other); String& operator+=(const String& other); String& operator=(const String& other); String operator()(int start, int end); char& operator[](int index); ~String(); };
//String.cpp
#include"String.h"
String::String(const char* s)
{
data = new char[strlen(s) + 1];
len = strlen(s);
strcpy(data, s);
data[len] = ' ';
}
String::String(const String& other)
{
len = other.len;
data = new char[len + 1];
strcpy(data, other.data);
}
String& String::operator+=(const String& other)
{
char* temp = data;
len += other.len;
data = new char[len + 1];
strcpy(data, temp);
strcat(data, other.data);
return *this;
// TODO: 在此处插入 return 语句
}
String& String::operator=(const String& other)
{
if (data != NULL)
{
delete[]data;
data = NULL;
}
data = new char[other.len + 1];
len = other.len;
strcpy(data, other.data);
return *this;
// TODO: 在此处插入 return 语句
}
String String::operator()(int start, int end)
{
String temp;
temp.data = new char[end - start + 1];
temp.len = end - start;
int j = 0;
for (int i = start; i < end; i++)
{
temp.data[j] = data[i];
j++;
}
temp.data[end - start] = ' ';
return temp;
}
char& String::operator[](int index)
{
return data[index];
// TODO: 在此处插入 return 语句
}
String::~String()
{
delete[]data;
}
String operator+(const String& s1, const String& s2)
{
char* p = new char[s1.len + s2.len + 1];
strcpy(p, s1.data);
strcat(p, s2.data);
return String(p);
}
ostream& operator<<(ostream& os, const String& obj)
{
os << obj.data;
return os;
// TODO: 在此处插入 return 语句
}
istream& operator>>(istream& is, String& obj)
{
obj.data = new char[512];//这里大小定的512
is >> obj.data;
obj.len = strlen(obj.data);
return is;
// TODO: 在此处插入 return 语句
}
bool operator>(const String& s1, const String& s2)
{
if (strcmp(s1.data, s2.data) > 0) return true;
else return false;
}
bool operator>=(const String& s1, const String& s2)
{
if (strcmp(s1.data, s2.data) >= 0) return true;
else return false;
}
bool operator==(const String& s1, const String& s2)
{
if (strcmp(s1.data, s2.data) == 0) return true;
else return false;
}
bool operator!=(const String& s1, const String& s2)
{
if (strcmp(s1.data, s2.data) != 0) return true;
else return false;
}
bool operator<(const String& s1, const String& s2)
{
if (strcmp(s1.data, s2.data) < 0) return true;
else return false;
}
bool operator<=(const String& s1, const String& s2)
{
if (strcmp(s1.data, s2.data) <= 0) return true;
else return false;
}
主函数部分就没有放上去啦。


![[c++]String类的实现(输入输出赋值比较大小运算符重载) [c++]String类的实现(输入输出赋值比较大小运算符重载)](http://www.mshxw.com/aiimages/31/853100.png)
