C++ 自己實現資料結構之 string類
阿新 • • 發佈:2019-02-04
需要自己實現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; }