自定义一个字符串类 mystring,成员函数仅在类的内部申明,在类的外部给出具体实现。
★成员数据:
① char s [ N ] ; 用于存放字符串的数组容器,其中 N 为常量
② int size ; 数组中最多可容纳的元素数, size = N
③ int last ; 数组中已用元素的最大下标,空串时 last = 0
c++的mystring类
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
9条回答 默认 最新
关注 头文件:
const int N = 1000; class mystring { friend ostream& operator<<(ostream& os, const mystring& str);//友元函数重载<< friend istream& operator>>(istream& is, mystring& str);//友元函数重载>> private: char s[N]; //存放字符串的数组容器 int size; //最大可用元素数,可防止数组出界,提高健壮性 int last; //已用元素最大下标 public: mystring(); mystring(char* str); mystring(mystring& st); ~mystring(); void show(); int length(); char& operator[](int i); mystring& operator=(const mystring&); mystring& operator=(char* st);//这里重载的=是把C风格字符串赋给mystring mystring operator+(mystring&); mystring operator+=(mystring&); bool operator<(mystring&); bool operator==(mystring&); };
cpp文件
mystring::mystring() { last = 0; size = N; s[last] = '\0'; } mystring::mystring(char* str) { last = 0; size = N; int len = strlen(str); if (len > size - 1) { len = size - 1; } strncpy(this->s, str, len); s[len] = '\0'; last = len; } mystring::mystring(mystring& st) { last = 0; size = N; int len = strlen(st.s); if (len > size - 1) { len = size - 1; } strncpy(this->s, st.s, len); s[len] = '\0'; last = len; } mystring::~mystring() { last = 0; size = N; } int mystring::length() { return last; } void mystring::show() { cout << s << endl; } char& mystring::operator[](int i) { //返回引用,可读可写 //if(i>last){cout<<"access violate!";exit(1);} return s[i]; } mystring& mystring::operator=(const mystring& st) { if (&st == this)return*this; last = st.last;//新的长度 last = 0; do { s[last] = st.s[last]; last++; } while (st.s[last] != '\0' && last < size - 1); s[last] = '\0'; //截尾处理时,必须加串结束符 return *this;//拷贝的临时变量生命期在调用它的表达式中 } mystring& mystring::operator=(char* s) { last = 0; do { this->s[last] = s[last]; last++; } while (this->s[last] != '\0' && last < size - 1); this->s[last] = '\0'; //截尾处理时,必须加串结束符 return *this; } mystring mystring::operator+(mystring& st) {//注意+和+=的不同 mystring temp(*this); temp.last = temp.last + st.last;//新的长度 temp.last = 0; do { temp.s[temp.last] = this->s[temp.last]; temp.last++; } while (this->s[temp.last] != '\0' && temp.last < temp.size - 1); if (temp.last < temp.size - 1) { int i = 0; do { temp.s[temp.last] = st.s[i]; temp.last++; i++; } while (st.s[i] != '\0' && temp.last < temp.size - 1); } temp.s[temp.last] = '\0'; //截尾处理时,必须加串结束符 return temp; } mystring mystring::operator+=(mystring& st) {//+=在对象自身进行 mystring temp(*this); int i;//保存原来的长度 last = last + st.last;//新的长度 last = 0; do { s[last] = temp.s[last]; last++; } while (temp.s[last] != '\0' && last < size - 1); if (last < size - 1) { i = 0; do { s[last] = st.s[i]; i++; last++; } while (st.s[i] != '\0' && last < size - 1); } s[last] = '\0'; //截尾处理时,必须加串结束符*/ return *this;//拷贝的临时变量生命期在调用它的表达式中 } bool mystring::operator<(mystring& st) { //重载<运算符 int i = 0, k; do { k = s[i] - st.s[i]; i++; } while (k == 0 && i < last && i < st.last); if (k < 0) return true; if (i == last && i != st.last) return true; return false; } bool mystring::operator==(mystring& st) { int i = 0, k; if (last != st.last) return false; do { k = s[i] - st.s[i]; i++; } while (k == 0 && i < last); if (k != 0) return false; else return true; } ostream& operator<<(ostream& os, const mystring& str) { os << str.s; return os; } istream& operator>>(istream& is, mystring& str) { char tem[N]; //简单的申请一块内存 is >> tem; str = tem; return is; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥100 支付宝网页转账系统不识别账号
- ¥15 基于单片机的靶位控制系统
- ¥15 AT89C51控制8位八段数码管显示时钟。
- ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
- ¥15 下图接收小电路,谁知道原理
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
- ¥15 ETLCloud 处理json多层级问题