强化练习-string类的实现 梦里梦外; 2022-03-18 12:36 159阅读 0赞 MyString.h #pragma once #include <iostream> using namespace std; class Mystring { public: Mystring(); Mystring(const char* str); Mystring(const Mystring& another); ~Mystring(); char& operator[](int index); friend ostream& operator<<(ostream& os, const Mystring& str); friend istream& operator>>(istream& is, Mystring& str); Mystring& operator=(const char* str); Mystring& operator=(const Mystring& str); bool operator==(const Mystring& another); Mystring operator+(const Mystring& another); private: int m_length; char* m_ptr; }; MyString.cpp #include "stdafx.h" #include "MyString.h" Mystring::Mystring() { m_length = 0; m_ptr = NULL; } Mystring::Mystring(const char* str) { int len = strlen(str); if (len > 0) { m_length = len; m_ptr = new char[len+1]; m_ptr[len] = 0; memcpy(m_ptr, str, len); } } Mystring::Mystring(const Mystring& another) { int len = another.m_length; if (len > 0) { m_length = len; m_ptr = new char[len + 1]; m_ptr[len] = 0; memcpy(m_ptr, another.m_ptr, len); } } Mystring::~Mystring() { if (m_ptr != NULL) { delete m_ptr; m_ptr = NULL; m_length = 0; } } char& Mystring::operator[](int index) { return m_ptr[index]; } ostream& operator<<(ostream& os, const Mystring& str) { os << str.m_ptr; return os; } istream& operator>>(istream& is, Mystring& str) { if (str.m_length == 0&& str.m_ptr==NULL) { char string_tmp[100] = { 0 }; cin >> string_tmp; int len = strlen(string_tmp); str.m_length = len; str.m_ptr = new char[len + 1]; str.m_ptr[len] = 0; memcpy(str.m_ptr, string_tmp, len); } return is; } Mystring& Mystring::operator=(const char* str) { int len = strlen(str); if (len > 0) { delete[] m_ptr; //把以前释放 m_length = len; m_ptr = new char[len + 1]; m_ptr[len] = 0; memcpy(m_ptr, str, len); return *this; } } Mystring& Mystring::operator=(const Mystring& str) { if (this == &str) return *this; int len = str.m_length; if (len > 0) { delete[] m_ptr; //把以前释放 m_length = len; m_ptr = new char[len + 1]; m_ptr[len] = 0; memcpy(m_ptr, str.m_ptr, len); return *this; } } bool Mystring::operator==(const Mystring& another) { if (this == &another)return true; int len = another.m_length; if (m_length == len && len > 0) { for (int i = 0; i < len; i++) { if (m_ptr[i] != another.m_ptr[i]) return false; } return true; } else return false; } Mystring Mystring::operator+(const Mystring& another) { Mystring tmp; if (another.m_length == 0) return *this; else { int all_len = m_length + another.m_length; char* new_ptr = new char [all_len + 1]; new_ptr[all_len] = 0; memcpy(new_ptr, m_ptr, m_length); memcpy(new_ptr + m_length, another.m_ptr, another.m_length); tmp.m_ptr = new_ptr; tmp.m_length = all_len; return tmp; } } user.cpp #include "stdafx.h" #include "MyString.h" int main() { Mystring str1("julian"); Mystring str2; cin >> str2; cout << str1 << " " << str2 << endl; str2 = str1; cout << str2 << endl; str2 = "fuck"; cout << str2 << endl; Mystring str3(str1); Mystring str4 = "mark"; if (str3 == str4) cout << "same" << endl; else cout << "no same" << endl; if (str3 == str1) cout << "same" << endl; else cout << "no same" << endl; str4[0] = 'a'; cout << str4 << endl; Mystring str5 = str1 + str4; str5[0] = 'w'; cout << str5 << endl; cout << str1 << endl; cout << str5 + str1 + str2 << endl; return 0; } 结果输入:kerr 输出: ![在这里插入图片描述][20190220212412439.png] [20190220212412439.png]: /images/20220318/19bc833a3c584797a119a6a6e3216b3a.png
还没有评论,来说两句吧...