需要自己实现C++中的string类。
本文给出的实现包括string类的一些基本操作。
如:构造 拷贝构造 赋值 判断相等 字符串长度 []操作 字符串拼接 <<序列化 swap操作等。
代码如下:
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
class String
{
public:
// 构造
String();
String(const char* str);
String(const String& other);
public:
String operator+(const String& other);
String& operator=(const String& other);
bool operator==(const String& otehr);
const char& operator[](unsigned int);
size_t size() const;
const char* c_str() const;
void swap(String& other);
public:
// 重载
friend ostream& operator<<(ostream& out, const String& other);
friend istream& operator>> (istream&, String&);
// 析构
~String(void);
private:
char *m_data; // 保存字符串值
};
//1
String::String()
{
m_data = new char[1];
*m_data = '\0';
}
//2
String::String(const char* other)
{
if (other == NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
m_data = new char[strlen(other) + 1];
strcpy(m_data, other);
}
}
//3
String::String(const String& other)
{
m_data = new char[other.size() + 1];
strcpy(m_data, other.c_str());
}
//4
String& String::operator=(const String& other)
{
if (this != &other)
{
delete[] m_data;
m_data = new char[other.size() + 1];
strcpy(m_data, other.c_str());
}
return *this;
}
//5
size_t String::size() const
{
return strlen(m_data);
}
//6
const char* String::c_str() const
{
return m_data;
}
//7
void String::swap(String& other)
{
std::swap(m_data, other.m_data);
}
//8
String::~String()
{
delete[] m_data;
}
//9
bool String::operator==(const String& other)
{
if (strlen(m_data) != other.size())
{
return false;
}
return strcmp(m_data, other.c_str()) ? false : true;
}
//10
String String::operator+(const String& other)
{
String resString;
if (!other.m_data)
{
resString = *this;
}
else if (!m_data)
{
resString = other;
}
else
{
resString.m_data = new char[other.size() + this->size() + 1];
strcpy(resString.m_data, m_data);
strcat(resString.m_data, other.c_str());
}
return resString;
}
//11
const char& String::operator[](unsigned int idx)
{
if (0 <= idx && idx <= this->size())
{
return m_data[idx];
}
else
{
//return '\0';
// 这里可以抛出异常
}
}
// 12
ostream& operator<<(ostream& out, const String& other)
{
out << other.c_str();
return out;
}
//13
istream& operator>> (istream& in, String& res)
{
char tmp[255];
in >> setw(255) >> tmp;
res = tmp;
return in;
}
void fun1(String s)
{
}
void fun2(const String& s)
{
}
String fun3()
{
String tmp("fun3");
return tmp;
}
int main(void)
{
String s0;
String s1("s1");
cout << "s1 = " << s1 << endl;
String s2(s0);
String s3 = s1;
s2 = s1;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
fun1(s1);
cout << "s1 = " << s1 << endl;
fun2(s1);
cout << "s1 = " << s1 << endl;
fun1("TEST FUN1");
cout << "s1 = " << s1 << endl;
fun2("TEST FUN2");
cout << "s1 = " << s1 << endl;
String s4 = fun3();
cout << "s4 = " << s4 << endl;
std::vector<String> svec;
svec.push_back(s0);
svec.push_back(s1);
svec.push_back(fun3());
svec.push_back("Last Element");
for (auto& v: svec)
{
cout << v << endl;
}
cout << "s1 + s3 = " << (s1 + s3) << endl;
if (s1 == s3)
{
cout << "s1 = s3" << endl;
}
else
{
cout << "s1 != s3" << endl;
}
cout << "s1[2] = " << s1[1] << endl;
swap(s1, String("s1 swap"));
cout << "s1 after swap = " << s1<<endl;
return 0;
}