当前位置: 首页 > news >正文

string 类运算符重载

目录

一、赋值运算符 (=)

二、复合赋值运算符 (+=)

三、字符串连接运算符 (+)

四、流操作运算符 (>> 和 <<)

工作原理:

五、关系比较运算符

boolalpha 的基本作用

六、总结表


一、赋值运算符 (=)

string 类重载了赋值运算符,支持多种类型的赋值操作。

string& operator=(const string& str);      // string对象赋值
string& operator=(const char* s);         // C风格字符串赋值
string& operator=(char c);                // 单个字符赋值
string& operator=(initializer_list<char> il);  // 初始化列表赋值 (C++11)

示例代码:

#include <iostream>
#include <string>
using namespace std;int main() {string s1;string s2("CSDN");// 1. string对象赋值s1 = s2;cout << s1 << endl;  // 输出: CSDN// 2. C风格字符串赋值s1 = "hello world";cout << s1 << endl;  // 输出: hello world// 3. 单个字符赋值s1 = 'A';cout << s1 << endl;  // 输出: A// 4. 初始化列表赋值 (C++11)s1 = {'C', 'P', 'P'};cout << s1 << endl;  // 输出: CPPreturn 0;
}


二、复合赋值运算符 (+=)

string 类重载了 += 运算符,支持多种类型的追加操作。

string& operator+=(const string& str);    // 追加string对象
string& operator+=(const char* s);       // 追加C风格字符串
string& operator+=(char c);              // 追加单个字符
string& operator+=(initializer_list<char> il);  // 追加初始化列表 (C++11)

示例代码:

#include <iostream>
#include <string>
using namespace std;int main() {string s1("Hello");string s2(" CSDN");// 1. 追加string对象s1 += s2;cout << s1 << endl;  // 输出: Hello CSDN// 2. 追加C风格字符串s1 += ", welcome!";cout << s1 << endl;  // 输出: Hello CSDN, welcome!// 3. 追加单个字符s1 += '!';cout << s1 << endl;  // 输出: Hello CSDN, welcome!!// 4. 追加初始化列表 (C++11)s1 += {' ', ':', ')'};cout << s1 << endl;  // 输出: Hello CSDN, welcome!! :)return 0;
}


三、字符串连接运算符 (+)

string 类重载了 + 运算符,支持多种字符串连接组合。

string operator+(const string& lhs, const string& rhs);  // string + string
string operator+(const string& lhs, const char* rhs);   // string + C字符串
string operator+(const char* lhs, const string& rhs);   // C字符串 + string
string operator+(const string& lhs, char rhs);          // string + 字符
string operator+(char lhs, const string& rhs);          // 字符 + string
string operator+(string&& lhs, string&& rhs);           // 右值引用版本

C++ 中的 string 类的 + 运算符被设计为不修改原对象,而是返回一个新的 临时string 对象

示例代码:

#include <iostream>
#include <string>
using namespace std;int main() {string s1("Super");string s2("Man");const char* cstr = "Woman";char ch = '!';// 1. string + stringcout << (s1 + s2) << endl;      // 输出: SuperMan// 2. string + C字符串cout << (s1 + cstr) << endl;    // 输出: SuperWoman// 3. C字符串 + stringcout << (cstr + s1) << endl;    // 输出: WomanSuper// 4. string + 字符cout << (s1 + ch) << endl;      // 输出: Super!// 5. 字符 + stringcout << (ch + s1) << endl;      // 输出: !Super// 6. 链式连接cout << (s1 + " " + s2 + ch) << endl;  // 输出: Super Man!return 0;
}


四、流操作运算符 (>> 和 <<)

string 类重载了流操作运算符,支持直接的输入输出。

istream& operator>>(istream& is, string& str);   // 输入运算符
ostream& operator<<(ostream& os, const string& str);  // 输出运算符

示例代码:

#include <iostream>
#include <string>
using namespace std;int main() {string name;cout << "请输入您的名字: ";cin >> name;  // 使用重载的>>运算符cout << "您好, " << name << "!" << endl;  // 使用重载的<<运算符// 读取一行输入cout << "请输入一句话: ";cin.ignore();  // 清除之前的换行符getline(cin, name);  // 使用getline读取整行cout << "您输入的是: " << name << endl;return 0;
}

工作原理:

  1. cin.ignore()

    • 清除输入缓冲区中残留的换行符(\n

    • 这是因为之前的 cin >> 操作会在缓冲区留下换行符

    • 如果不调用,getline 会立即读取到空行

  2. getline(cin, name)

    • 读取从当前光标到换行符之前的所有字符

    • 包含空格

    • 丢弃最后的换行符(不存入字符串)


五、关系比较运算符

string 类重载了完整的关系运算符,支持多种比较操作。

bool operator==(const string& lhs, const string& rhs);
bool operator!=(const string& lhs, const string& rhs);
bool operator<(const string& lhs, const string& rhs);
bool operator<=(const string& lhs, const string& rhs);
bool operator>(const string& lhs, const string& rhs);
bool operator>=(const string& lhs, const string& rhs);

比较规则:

  1. 按字典序逐个比较字符的ASCII码值

  2. 比较区分大小写

  3. 较短的字符串如果与较长字符串的前面部分相同,则认为较短字符串较小

示例代码:

#include <iostream>
#include <string>
using namespace std;int main() {string s1("apple");string s2("banana");string s3("Apple");string s4("app");// 1. 相等比较cout << boolalpha;cout << (s1 == "apple") << endl;    // 输出: truecout << (s1 == s3) << endl;         // 输出: false// 2. 不等比较cout << (s1 != s2) << endl;         // 输出: true// 3. 小于比较cout << (s1 < s2) << endl;          // 输出: true ('a' < 'b')cout << (s1 < s3) << endl;          // 输出: false ('a' > 'A')cout << (s4 < s1) << endl;          // 输出: true ("app" < "apple")// 4. 其他比较cout << (s1 <= s2) << endl;         // 输出: truecout << (s1 > s3) << endl;          // 输出: truecout << (s1 >= "apple") << endl;    // 输出: truereturn 0;
}

boolalpha 的基本作用

功能:将布尔值的输出从 0/1 格式转换为 true/false 文本格式

默认情况(不使用 boolalpha):

bool b = true;
cout << b;  // 输出: 1

使用 boolalpha 后

cout << boolalpha;
bool b = true;
cout << b;  // 输出: true


六、总结表

运算符功能描述支持的操作类型
=赋值string, const char*, char, initializer_list
+=追加string, const char*, char, initializer_list
+连接string + string, string + const char, const char + string, string + char, char + string
>> <<流操作输入输出流
== !=相等比较string与string, string与const char*
< <= > >=关系比较string与string, string与const char*
[]下标访问非const和const版本
bool空值检查隐式转换为bool

这些运算符重载使得string类的使用更加直观和方便,大大简化了字符串操作的代码编写。

http://www.lryc.cn/news/618910.html

相关文章:

  • Win10系统Ruby+Devkit3.4.5-1安装
  • qt界面优化--api绘图
  • SpringBoot项目限制带参数接口配置使用数量实现
  • php+apache+nginx 更换域名
  • 力扣.870优势洗牌解决方法: 下标排序​编辑力扣.942增减字符串匹配最长回文子序列牛客.背包问题(最大体积)力扣.45跳跃游戏II 另一种思考
  • 牛客疑难题(6)
  • Transformer的编码器与解码器模块深度解析及python实现完整案例
  • 树:数据结构中的层次架构
  • 前端基础知识NodeJS系列 - 06( Node 中的 Stream 的理解?应用场景?)
  • 【154页PPT】某大型再生资源集团管控企业数字化转型SAP解决方案(附下载方式)
  • 【从零开始java学习|第三篇】变量与数据类型的关联
  • 扣子空间深度解析
  • Apache 服务器基础配置与虚拟主机部署
  • CentOS 7.9 升级 GLibc 2.34
  • (C++)继承全解析及运用
  • Java 大视界 -- Java 大数据在智能教育学习效果评估指标体系构建与精准评估中的应用(394)
  • 教程 | 用Parasoft SOAtest实现高效CI回归测试
  • Day02——Docker
  • 一体化步进伺服电机在无人机舱门应用中的应用案例
  • 书籍数组中未出现的最小正整数(8)0812
  • 小白挑战一周上架元服务——ArkUI04
  • Ubuntu与Rocky系统安装Java全指南
  • C# 基于halcon的视觉工作流-章29-边缘提取-亚像素
  • 深入理解数据库架构:从原理到实践的完整指南
  • 力扣47:全排列Ⅱ
  • ffmpeg,ffplay, vlc,rtsp-simple-server,推拉流命令使用方法,及测试(二)
  • Linux内核编译ARM架构 linux-6.16
  • 深度贴:前端网络基础及进阶(3)
  • archlinux中VLC无法播放视频的解决办法
  • Linux TC流控实现机制