C++写一个string类,其实更多的是对C++内存分配,运算符重载的功能的运用。
MyString.h
#pragma once #includeusing namespace std; class MyString { public: MyString(); //默认构造函数 MyString(int n, char c); //普通构造函数 MyString(const char* source); //普通构造函数 MyString(const MyString& s); //复制构造函数 MyString& operator=(char* s); //重载=,实现字符串赋值 MyString& operator=(const MyString& s); //重载=,实现对象赋值 ~MyString(); //析构函数 char& operator[](int i); //重载[],实现数组运算 const char& operator[](int i) const; // 重载[],实现数组运算,对象为常量 MyString& operator+=(const MyString& s); //重载+=,实现与对象相加 MyString& operator+=(const char* s); //重载+=,实现与字符串相加 friend ostream& operator<<(ostream& out, MyString& s); //重载<< friend istream& operator>>(istream& in, MyString& s); //重载>> friend bool operator<(const MyString& left, const MyString& right); //重载< friend bool operator>(const MyString& left, const MyString& right); //重载> friend bool operator==(const MyString& left, const MyString& right); //重载== friend bool operator!=(const MyString& left, const MyString& right); //重载!= char* getData(); int length(); private: int size; //data表示的字符串长度 char* data; //指向字符串数据 };
MyString.cpp
#include "MyString.h" MyString::MyString() { data = new char[1]; *data = '\0'; size = 0; } MyString::MyString(int n, char c) { size = n; data = new char[n + 1]; char* temp = data; while (n--) { *temp++ = c; } *temp = '\0'; } MyString::MyString(const char* source) { if (source == nullptr) { data = new char[1]; *data = '\0'; size = 0; } else { size = strlen(source); data = new char[size + 1]; strcpy_s(data, size + 1, source); } } MyString::MyString(const MyString& s) { size = s.size; data = new char[s.size + 1]; strcpy_s(data, s.size + 1, s.data); } MyString& MyString::operator=(char* s) { if (data != nullptr) delete[] data; size = strlen(s); data = new char[size + 1]; strcpy_s(data, size + 1, s); return *this; } MyString& MyString::operator=(const MyString& s) { if (this == &s) return *this; if (data != nullptr) //先释放内存 delete[] data; size = s.size; data = new char[size + 1]; strcpy_s(data, size + 1, s.data); return *this; } MyString::~MyString() { if (data != nullptr) { delete[] data; data = nullptr; size = 0; } } char& MyString::operator[](int i) { return data[i]; } const char& MyString::operator[](int i) const { return data[i]; } MyString& MyString::operator+=(const MyString& s) { int len = size + s.size + 1; char* temp = data; data = new char[len]; strcpy_s(data, size + 1, temp); strcat_s(data, len, s.data); delete[] temp; size = len - 1; return *this; } MyString& MyString::operator+=(const char* s) { if (s == nullptr) { return *this; } int len = size + strlen(s) + 1; char* temp = data; data = new char[len]; strcpy_s(data, size + 1, temp); strcat_s(data, len, s); delete[] temp; size = len - 1; return *this; } char* MyString::getData() { return data; } int MyString::length() { return size; } ostream& operator<<(ostream& out, MyString& s) { for (int i = 0; i < s.size; i++) out << s[i] << " "; return out; } istream& operator>>(istream& in, MyString& s) { char p[50]; in.getline(p, 50); //从输入流接受最多50个字符 s = p; //调用赋值函数 return in; } bool operator<(const MyString& left, const MyString& right) //重载< { int i = 0; while (left[i] == right[i] && left[i] != 0 && right[i] != 0) i++; return left[i] - right[i] < 0 ? true : false; } bool operator>(const MyString& left, const MyString& right) //重载< { int i = 0; while (left[i] == right[i] && left[i] != 0 && right[i] != 0) i++; return left[i] - right[i] > 0 ? true : false; } bool operator==(const MyString& left, const MyString& right) { if (left.size != right.size) return false; for (int i = 0; i < left.size; i++) { if (left.data[i] != right.data[i]) return false; } return true; } bool operator!=(const MyString& left, const MyString& right) { if (left.size != right.size) return true; for (int i = 0; i < left.size; i++) { if (left.data[i] != right.data[i]) return true; } return false; }
在main方法里测试MyString的功能:
#include#include "MyString.h" using namespace std; int main() { MyString str(3, 'a'); MyString str1(str); MyString str2("asdf"); MyString str3; cout << "str: " << str << endl; cout << "str1: " << str1 << endl; cout << "str2: " << str2 << endl; cout << "str3: " << str3 << endl; str3 = str2; cout << "str3: " << str3 << endl; str3 = "12ab"; cout << "str3: " << str3 << endl; cout << "str3[2]: " << str3[2] << endl; str3 += "111"; cout << "str3: " << str3 << endl; str3 += str1; cout << "str3: " << str3 << endl; //键盘输入 cin >> str1; cout << "str1: " << str1 << endl; MyString t1 = "1234"; MyString t2 = "1234"; MyString t3 = "12345"; MyString t4 = "12335"; cout << "t1 == t2 ? " << (t1 == t2) << endl; cout << "t1 < t3 ? " << (t1 < t3) << endl; cout << "t1 > t4 ? " << (t1 > t4) << endl; cout << "t1 != t4 ? " << (t1 != t4) << endl; return 0; }
运行结果
以上是string类的大致实现。