日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++字符串处理操作符重载

發(fā)布時間:2025/3/15 c/c++ 10 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++字符串处理操作符重载 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

&& || 不能進(jìn)行運算符重載,因為運算符重載不能進(jìn)行截斷操作
截斷操作就是當(dāng) a || b,a為真的時候就不會再判斷b了,但是運算符重載不能達(dá)到這效果。

類定義

// 類定義#include <iostream> using namespace std;//c中沒有字符串 字符串類(c風(fēng)格的字符串) //空串 "" class MyString {friend ostream& operator<<(ostream &out, MyString &s);friend istream& operator>>(istream &in, MyString &s); public:MyString(int len = 0);MyString(const char *p);MyString(const MyString& s);~MyString();public: //重載=號操作符MyString& operator=(const char *p);MyString& operator=(const MyString &s);char& operator[] (int index);public: //重載 == !== bool operator==(const char *p) const;bool operator==(const MyString& s) const;bool operator!=(const char *p) const;bool operator!=(const MyString& s) const;public:int operator<(const char *p);int operator>(const char *p);int operator<(const MyString& s);int operator>(const MyString& s);//把類的指針 露出來 public:char *c_str(){return m_p;}const char *c_str2(){return m_p;}int length(){return m_len;} private:int m_len;char *m_p;};
  • << 和 >> 操作符重載
ostream& operator<<(ostream &out, MyString &s) {out<<s.m_p;return out; }istream& operator>>(istream &in, MyString &s) {cin>>s.m_p;return in; }
  • copy構(gòu)造函數(shù)
//拷貝構(gòu)造函數(shù) //MyString s3 = s2;MyString::MyString(const MyString& s) {this->m_len = s.m_len;this->m_p = new char[m_len +1];strcpy(this->m_p, s.m_p); }
  • =運算符重載
s4 = "s2222"; MyString& MyString::operator=(const char *p) {//1 舊內(nèi)存釋放掉if (m_p != NULL){delete [] m_p;m_len = 0;}//2 根據(jù)p分配內(nèi)存if (p == NULL){m_len = 0;m_p = new char[m_len + 1];strcpy(m_p, "");}else{m_len = strlen(p);m_p = new char[m_len + 1];strcpy(m_p, p);}return *this; }// s4 = s2; MyString& MyString::operator=(const MyString &s) {//1 舊內(nèi)存釋放掉if (m_p != NULL){delete [] m_p;m_len = 0;}//2 根據(jù)s分配內(nèi)存m_len = s.m_len;m_p = new char[m_len + 1];strcpy(m_p, s.m_p);return *this; }

[]運算符重載

char& MyString::operator[](int index) {return m_p[index]; }

== 和 !=運算符重載

//if (s2 == "s222222") bool MyString::operator==(const char *p) const {if (p == NULL){if (m_len == 0){return true;}else{return false;}}else{if (m_len == strlen(p)){return !strcmp(m_p, p);}else{return false;}} }bool MyString::operator!=(const char *p) const {return !(*this == p); }bool MyString::operator==(const MyString& s) const {if (m_len != s.m_len){return false;}return !strcmp(m_p, s.m_p); }bool MyString::operator!=(const MyString& s) const {return !(*this == s); }

> 和<操作符重載

//if (s3 < "bbbb") int MyString::operator<(const char *p) {return strcmp(this->m_p , p); }int MyString::operator>(const char *p) {return strcmp(p, this->m_p); }int MyString::operator<(const MyString& s) {return strcmp(this->m_p , s.m_p); }int MyString::operator>(const MyString& s) {return strcmp(s.m_p, m_p); }

總結(jié)

以上是生活随笔為你收集整理的C++字符串处理操作符重载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。