C++ note--string类的实现
来源:互联网 发布:现在还能开淘宝店吗 编辑:程序博客网 时间:2024/06/03 22:41
不引用标准库string的基础上实现string类的基本功能:
class MyString{public:MyString(const char *str = NULL); //普通构造函数MyString(const MyString &other);//拷贝构造函数~MyString(void);//析构函数MyString & operator =(constMyString &other);//赋值函数MyString & operator =(const char *str);private:char *m_data;//保存字符串};MyString::MyString(const char *str){if (str == NULL){m_data = new char[1];*m_data = '\0';}else{int length = strlen(str);m_data = new char[length];strcpy(m_data, str);}}MyString::MyString(const MyString &other){int length = strlen(other.m_data);m_data = new char[length+1]; // 最后一位为\0strcpy(m_data, other.m_data);}MyString::~MyString(void){delete []m_data;}MyString & MyString::operator=(const MyString &other){//检查自赋值if (this == &other)return *this;int length = strlen(other.m_data);m_data = new char[length+1];// 最后一位为\0strcpy(m_data, other.m_data);return *this;}MyString & MyString::operator=(const char *str){if (strcmp((*this).m_data, str) == 0)return *this;int length = strlen(str);m_data = new char[length + 1];strcpy(m_data, str);return *this;}
1 0
- C++ note--string类的实现
- C#note 03: string
- [C++]String类的实现
- [C++]String类的实现
- [C++]String类的实现
- C++String类的实现
- 【c++】string类的实现
- C++string类的实现
- C++string类的实现
- C++String类的实现
- C++string类的实现
- C/C++string类的实现
- C++string类的一个实现
- C++string类的简单实现
- c++string类的简单实现
- C++—String类接口的实现
- 【C/C++】String类实现
- C string buffer的实现
- WIKIOI--1201最小数和最大数
- P07 (**) 铺开(flatten) 嵌套列表
- Marklogic学习 由浅入深(6)—— XQuery增删改查
- Android笔记(7)---WebView的使用
- LeetCode 67:Add Binary
- C++ note--string类的实现
- hiho 1228(Mission Impossible 6-模拟文本编辑器)
- The Go Programming Language(读书笔记)
- WIKIOI--1202求和
- Codeforces Educational Codeforces Round 3 B. The Best Gift(签到)
- Swift 集合类型(笔记)
- Codeforces 609D Gadgets for dollars and pounds 二分答案
- Codeforces Educational Codeforces Round 3 C. Load Balancing(贪心)
- 汇编—输出字符