C++string接口(下)|修改器、字符串操作、成员常量、非成员函数重载

文章目录

  • [1. 修改器](#1. 修改器)
    • [1.1 追加内容](#1.1 追加内容)
      • [1.1.1 operator+=](#1.1.1 operator+=)
      • [1.1.2 append](#1.1.2 append)
      • [1.1.3 push_back](#1.1.3 push_back)
    • [1.2 赋值替代](#1.2 赋值替代)
    • [1.3 插入与删除](#1.3 插入与删除)
      • [1.3.1 insert](#1.3.1 insert)
      • [1.3.2 erase](#1.3.2 erase)
      • [1.3.3 pop_back](#1.3.3 pop_back)
    • [1.4 交换](#1.4 交换)
      • [成员函数 swap()](#成员函数 swap())
  • [2. 字符串操作](#2. 字符串操作)
    • [2.1 转换为 C 风格](#2.1 转换为 C 风格)
      • [2.1.1 c_str 与 data](#2.1.1 c_str 与 data)
      • [2.1.2 c_str 与 data的区别](#2.1.2 c_str 与 data的区别)
      • [2.1.2 生命周期警告:](#2.1.2 生命周期警告:)
    • [2.2 复制到缓冲区](#2.2 复制到缓冲区)
    • [2.3 查找系列(find 家族)](#2.3 查找系列(find 家族))
    • [2.3 子串提取](#2.3 子串提取)
    • [2.4 比较](#2.4 比较)
      • [2.4.1 compare](#2.4.1 compare)
  • [3. 成员常量(npos)](#3. 成员常量(npos))
  • [4. 非成员函数重载](#4. 非成员函数重载)
    • [4.1 拼接运算符](#4.1 拼接运算符)
    • [4.2 关系运算符](#4.2 关系运算符)
      • [4.2.1 ==、!=、<、<=、>、>=](#4.2.1 ==、!=、<、<=、>、>=)
      • [4.2.2 compare 和关系运算符的区别](#4.2.2 compare 和关系运算符的区别)
    • [4.3 输入输出](#4.3 输入输出)
    • [4.4 全局 std::swap](#4.4 全局 std::swap)
      • [4.4.1 全局 std::swap](#4.4.1 全局 std::swap)
      • [4.4.2 全局 std::swap与成员函数 swap 的关系](#4.4.2 全局 std::swap与成员函数 swap 的关系)

1. 修改器

1.1 追加内容

1.1.1 operator+=

cpp 复制代码
string s = "hello string";
string in = "123";


//+=	末尾插入
//插入string对象
s += in;
//s;hello string123
cout << s << endl;

s = "hello string";

//插入字符串
s += "hehe";
//s;hello stringhehe
cout << s << endl;

s = "hello string";

//插入字符
s += 'x';
//s;string
cout << s << endl;

1.1.2 append

cpp 复制代码
	s = "hello string";

	//append	末尾插入
	//插入string对象
	s.append(in);
	//s;hello string123
	cout << s << endl;

	s = "hello string";

	//插入subpos位置开始,sublen长的string对象,sublen可省
	s.append(in, 1, 2);
	//s;hello string23
	cout << s << endl;

	s = "hello string";

	//插入字符串
	s.append("1234");
	//s;hello string1234
	cout << s << endl;

	s = "hello string";

	//插入字符串前n个
	s.append("12345", 2);
	//s;hello string12
	cout << s << endl;

	s = "hello string";

	//插入n个字符
	s.append(4, 's');
	//s;hello stringssss
	cout << s << endl;

	s = "hello string";

1.1.3 push_back

cpp 复制代码
	s = "hello string";

	//push_back	尾插字符
	s.push_back('c');
	//s;hello stringc
	cout << s << endl;

1.2 赋值替代

assign

cpp 复制代码
	string s = "hello string";
	string a = "456";

	//assign	重新分配	清元素重新赋值
	//原容量 < 新元素个数 扩容
	//原容量 > 新元素个数 容量不变

	//用string对象赋值
	s.assign(a);
	//s;456
	cout << s << endl;

	s = "hello string";

	//从subpos位置开始,sublen长的string对象赋值,sublen可省
	s.assign(a, 1, 2);
	//s;56
	cout << s << endl;

	s = "hello string";

	//用字符串赋值
	s.assign("1234");
	//s;1234
	cout << s << endl;

	s = "hello string";

	//用字符串前n个赋值
	s.assign("12345", 2);
	//s;12
	cout << s << endl;

	s = "hello string";

	//用n个字符赋值
	s.assign(4, 's');
	//s;ssss
	cout << s << endl;

1.3 插入与删除

1.3.1 insert

cpp 复制代码
	string s = "hello string";
	string in = "123";

	//插入整个 string 对象
	s.insert(5, in);
	// s: hello123 string
	cout << s << endl;

	s = "hello string";

	//插入 string 对象的子串 ,从 subpos 开始,sublen 长
	s.insert(5, in, 1, 2);
	// s: hello23 string
	cout << s << endl;

	s = "hello string";

	//插入 C 风格字符串
	s.insert(5, "ABC");
	// s: helloABC string
	cout << s << endl;

	s = "hello string";

	//插入 C 风格字符串的前 n 个字符
	s.insert(5, "ABCDE", 3);
	// s: helloABC string
	cout << s << endl;

	s = "hello string";

	//插入 n 个重复字符
	s.insert(5, 3, '*');
	// s: hello*** string
	cout << s << endl;

1.3.2 erase

cpp 复制代码
	//erase	删除元素
	//从pos位置,len长删除,都不给值全删
	s.erase(6, 7);
	//s;hello

1.3.3 pop_back

cpp 复制代码
	//pop_back	尾删一个元素
	s.pop_back();
	//s;1234

1.4 交换

成员函数 swap()

cpp 复制代码
	//swap		交换
	s.swap(h);
	//s;hello
	//h;string
	cout << s << endl;
	cout << h << endl;

2. 字符串操作

2.1 转换为 C 风格

2.1.1 c_str 与 data


cpp 复制代码
	string s = "hello string";
	//c_str		转换为c风格只读字符串	
	const char* carr = s.c_str();
	//carr;hello string
	cout << carr << endl;

	//data		同上
	const char* darr = s.c_str();
	//darr;hello string
	cout << darr << endl;

2.1.2 c_str 与 data的区别

C++ 11 前

  • c_str(): 始终保证返回一个以空字符 (\0) 结尾的 C 风格字符串
  • data(): 不保证返回的字符数组以空字符结尾

C++11后

两者完全等价,都返回带 \0 的指针

2.1.2 生命周期警告:

返回的指针在 string 对象修改或析构后立即失效

2.2 复制到缓冲区

copy

cpp 复制代码
	//copy		从string对象的len长,从pos(可省,pos = 0)位置,
	// 拷贝到字符串
	char ch[4] = { 0 };
	//需初始化, 需多一个空间存\0,否则会越界
	s.copy(ch, 3, 6);
	//ch;str\0
	cout << ch << endl;

copy需手动补 \0

2.3 查找系列(find 家族)

2.2.1正向/反向查找子串

find
cpp 复制代码
	string s = "hello string";
	string f = "o ";

	//find				正向查找,返回值size_t类型,
	//成功返回要查找的首元素在被找中对象对应的地址下标,
	//失败返回npos
	//用string对象,在pos位置(pos = 0)开始查找
	size_t i = s.find(f,1);
	//i;4
	cout << i << endl;

	//用字符串,在pos位置(pos = 0)开始查找
	i = s.find("st", 3);
	//i;6
	cout << i << endl;

	//用字符串,在pos位置(pos = 0),用字符串前n个查找
	i = s.find("lll", 1, 2);
	//i;2
	cout << i << endl;

	//用字符,在pos位置(pos = 0)查找
	i = s.find("i", 3);
	//i;9
	cout << i << endl;
rfind
cpp 复制代码
	//rfind				反向查找,返回值size_t类型,
	//成功返回要查找的首元素在被找中对象对应的地址下标,
	//失败返回npos
	//用string对象,在pos位置(pos = npos)开始查找
	i = s.rfind(f, 1);
	//i;4
	cout << i << endl;

	//用字符串,在pos位置(pos = npos)开始查找
	i = s.rfind("st", 3);
	//i;6
	cout << i << endl;

	//用字符串,在pos位置(pos = npos),用字符串前n个查找
	i = s.rfind("lll", 1, 2);
	//i;2
	cout << i << endl;

	//用字符,在pos位置(pos = npos)查找
	i = s.rfind("i", 3);
	//i;9
	cout << i << endl;

2.2.2 查找字符集合中任一字符

find_first_of

find_first_of正向查找,返回值size_t类型,

查找对象的每个元素在被找对象中第一次出现的下标

返回,同上

用法,同上find

find_last_of

find_last_of正向查找,返回值size_t类型,

查找对象的每个元素在被找对象中最后一次出现的下标

返回,同上

用法,同上find

2.2.3 查找不在集合中的字符

find_first_not_of

find_first_not_of 正向查找,返回值size_t类型,

查找对象的每个元素在被找对象中第一次没出现的下标

返回,同上

用法,同上find

find_last_not_of

find_first_not_of 正向查找,返回值size_t类型,

查找对象的每个元素在被找对象中最后一次没出现的下标

返回,同上

用法,同上find

返回值处理:与 std::string::npos 比较(npos 为 static const size_t,值为 -1)

2.3 子串提取

substr

cpp 复制代码
string s = "hello string";

//substr	返回从原对象pos位置(可省,pos = 0),
// npos长(可省,npos = -1)的string拷贝
string s1 = s.substr(6, 3);
//s1;str
cout << s1 << endl;

2.4 比较

2.4.1 compare

cpp 复制代码
// compare:按ASCII码值比较字符串(可指定子串)
// 返回值:相等返回 0;小于返回负数;大于返回正数
	//......

3. 成员常量(npos)

static const size_t npos = -1; 其值为无符号整型的最大值

在string相关函数中常常用来访问末尾元素


4. 非成员函数重载

4.1 拼接运算符

operator+

cpp 复制代码
	//+		拼接	可string对象\字符串\字符,返回拼接后string新对象,
	//......

4.2 关系运算符

4.2.1 ==、!=、<、<=、>、>=

cpp 复制代码
	//比较操作符重载	返回bool类
	//...

4.2.2 compare 和关系运算符的区别

对比项 compare 关系运算符
返回值 整数 (负数、零、正数) 布尔值 (true / false)
核心语义 判断"是或否" 判断"顺序先后"或"相等程度"
返回值不同

compare 返回 int,适合需要三路结果的场景(如排序、switch 分支)。

关系运算符返回 bool(true / false),适合直接写在 if 条件中。

子串支持不同

compare 可以直接指定子串进行比较(如 s.compare(pos, len, other)),避免创建临时对象。

关系运算符只能比较整个字符串,要比较子串必须先调用 substr,会产生额外拷贝。

4.3 输入输出

4.3.1 输入

operator>>
cpp 复制代码
	//流提取重载>>	输入字符 遇到空格或换行停终止
	string s;
	cin >> s;
std::getline
cpp 复制代码
	//getline	自定义终止符(可省,默认为换行)
	getline(cin,s,'*');
	//遇到*停下

4.3.2 输出

operator<<
cpp 复制代码
	//流插入重载<<	打印字符
	cout << s << endl;

4.4 全局 std::swap

4.4.1 全局 std::swap

cpp 复制代码
	//非成员swap			用法同成员swap
	swap(s, a);
	//s;123
	//a;hello world
	cout << s << endl;
	cout << a << endl;

4.4.2 全局 std::swap与成员函数 swap 的关系

作用
成员函数 swap 直接操作内部成员,实现指针交换,运行高效
非成员函数 swap 调用成员函数 swap,实现 swap(a,b) 方式
全局 std::swap 泛型交换,实现所有同类型数据的交换
相关推荐
AbandonForce2 小时前
C++ 多态(多态定义 多态应用 多态底层||final override关键字||抽象类)
java·开发语言·c++
进击的荆棘2 小时前
C++起始之路——unordered_map和unordered_set的使用
开发语言·c++·stl·unordered_map·unordered_set
进击的荆棘2 小时前
C++起始之路——封装红黑树实现map和set
c++·stl·set·map
云深麋鹿2 小时前
C++ | 模板
开发语言·c++
t***54410 小时前
Clang 编译器在 Orwell Dev-C++ 中的局限性
开发语言·c++
oy_mail11 小时前
QoS质量配置
开发语言·智能路由器·php
oyzz12011 小时前
PHP操作redis
开发语言·redis·php
nashane11 小时前
HarmonyOS 6学习:网络能力变化监听与智能提示——告别流量偷跑,打造贴心网络感知应用
开发语言·php·harmony app
yolo_guo12 小时前
redis++使用: hmset 与 hmget
c++·redis