C++实现string类
2021/6/2 12:22:59
本文主要是介绍C++实现string类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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类的大致实现。
这篇关于C++实现string类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享