NO.28十六届蓝桥杯备战|string|insert|find|substr|关系运算|stoi|stol|stod|stof|to_string(C++)

insert

如果我们需要在字符串中间的某个位置插⼊⼀个字符串,得掌握⼀个函数就是insert

string& insert (size_t pos, const string& str); 
//pos位置前⾯插⼊⼀个string字符串  
string& insert (size_t pos, const char* s); 
//pos位置前⾯插⼊⼀个C⻛格的字符串  
string& insert (size_t pos, size_t n, char c);
//pos位置前⾯插⼊n个字符c

![[Pasted image 20250306211142.png]]

#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string s = "abcdefghi";  
	string str = "xxx";  
	cout << s << endl;  
	s.insert(3, str);  
	cout << s << endl;  
	return 0;  
} 

#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string s = "abcdefghi";  
	cout << s << endl;  
	s.insert(3, "xxx");  
	cout << s << endl;  
	return 0;  
} 

#include <iostream>
#include <string>  
using namespace std;  
int main()  
{  
	string s = "abcdefghi";  
	cout << s << endl;  
	s.insert(3, 3, 'x');  
	cout << s << endl;  
	return 0;  
}
find()

find() 函数⽤于查找字符串中指定⼦串/字符,并返回⼦串/字符在字符串中第⼀次出现的位置
![[Pasted image 20250306211421.png]]

size_t find (const string& str, size_t pos = 0) const;  
//查找string类型的字符串str,默认是从头开始查找,pos可以指定位置开始  
size_t find (const char* s, size_t pos = 0) const;  
//查找C⻛格的字符串s,默认是从头开始查找,pos可以指定位置开始  
size_t find (const char* s, size_t pos, size_t n) const;  
//在字符串的pos这个位置开始查找C⻛格的字符串s中的前n个字符,  
size_t find (char c, size_t pos = 0) const;  
//查找字符c,默认是从头开始,pos可以指定位置开始
  • 若找到。返回⼦串/字符在字符串中第⼀次出现的起始下标位置。
  • 若未找到。返回⼀个整数值 npos (针对 npos 的介绍会在下⾯给出)。通常判断 find() 函数的返回值是否等于 npos 就能知道是否查找到⼦串或者字符。
//代码1  
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	string str = "llo";  
	//查找string类型的字符串  
	size_t n = s.find(str);  
	cout << n << endl;  
	n = s.find(str, n + 1); //从n+1这个指定位置开始查找  
	cout << n << endl;  
	//查找C⻛格的字符串  
	n = s.find("llo");  
	cout << n << endl;  
	n = s.find("llo", n + 1); //从n+1这个指定位置开始查找  
	cout << n << endl;  
	
	return 0;  
}  

//代码2  
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	//在s中,0这个指定位置开始查找"word"中的前3个字符  
	size_t n = s.find("word", 0, 3);  
	cout << n << endl;  
	n = s.find("everyday", n+1, 5);  
	cout << n << endl;
	
	return 0;  
}  

//代码3  
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	size_t n = s.find('o');  
	cout << n << endl;  
	n = s.find('o', n + 1);  
	cout << n << endl;  
	
	return 0;  
}  

//查找不到的情况  
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	string str = "bit";  
	size_t n = s.find(str);  
	cout << n << endl;  
	if(n != string::npos)  
	cout << "找到了,位置是:" << n << endl;  
	else  
	cout << "没有找到" << endl;  
	
	return 0;  
}

在字符串中查找字符或者字符串时,有可能查找不到,这时候 find 函数会返回 npos 这个值,该数字并不是⼀个随机的数字,⽽是 string 中定义的⼀个静态常量 npos 。我们通常会判断 find 函数的返回值是否等于 npos 来判断,查找是否成功

static const size_t npos = -1;
#include <iostream>  
#include <string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	//注意:npos是string中定义的,使⽤npos需要带上string::指明是string类中的  
	cout << "npos:" << string::npos << endl;  
	return 0;  
}

![[Pasted image 20250306212530.png]]

substr()

substr() 函数⽤于截取字符串中指定位置指定⻓度的⼦串。函数原型如下

string substr (size_t pos = 0, size_t len = npos) const;  
//pos 的默认值是0,也就是从下标为0的位置开始截取  
//len 的默认值是npos,意思是⼀直截取到字符串的末尾
  • substr() :如果函数不传参数,就是从下标为0的位置开始截取,直到结尾,得到的是整个字符串;
  • substr(pos) :从指定下标 pos 位置开始截取⼦串,直到结尾;
  • substr(pos, len) :从指定下标 pos 位置开始截取⻓度为 len 的⼦串
    ![[Pasted image 20250306213426.png]]

返回值类型: string ,返回的是截取到的字符串,可以使⽤ string 类型的字符串接收

#include <iostream>  
#include<string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	string s1 = s.substr(7);  
	cout << s1 << endl;  
	string s2 = s.substr(7, 6);  
	cout << s2 << endl;
	
	return 0;  
}

![[Pasted image 20250306214201.png]]

substr() 和 find() 经常是配合使⽤的, find 负责找到位置, substr 从这个位置向后获得字符串

#include <iostream>  
#include<string> //添加string头⽂件  
using namespace std;  
int main()  
{  
	string s = "hello world hello everyone";  
	size_t n = s.find("world");  
	string s2 = s.substr(n, 10);  
	cout << s2 << endl;  
	
	return 0;  
}

![[Pasted image 20250306214235.png]]

string的关系运算

在实际写代码的过程中经常会涉及到两个字符串⽐较⼤⼩,⽐如:判断你输⼊的密码是否正确,就得将输⼊的密码和数据库中正确的密码⽐较。
那么两个 string 类型字符串是否可以⽐较⼤⼩呢?其实是可以的,C++中为string提供了⼀系列的关系运算。

⽀持的关系运算
string s1 = "abc";  
string s2 = "abcd";  
char s3[] = "abcdef"; //C⻛格的字符串  
(1) s1 == s2  
bool operator== (const string& lhs, const string& rhs);//使⽤⽅式:s1 == s2  
bool operator== (const char* lhs, const string& rhs);//使⽤⽅式:s3 == s1  
bool operator== (const string& lhs, const char* rhs);//使⽤⽅式:s1 == s3  
(2) s1 != s2  
bool operator!= (const string& lhs, const string& rhs);//使⽤⽅式:s1 != s2  
bool operator!= (const char* lhs, const string& rhs);//使⽤⽅式:s3 != s1  
bool operator!= (const string& lhs, const char* rhs);//使⽤⽅式:s1 != s3  
(3) s1 < s2  
bool operator< (const string& lhs, const string& rhs);//使⽤⽅式:s1 < s2  
bool operator< (const char* lhs, const string& rhs);//使⽤⽅式:s3 < s1  
bool operator< (const string& lhs, const char* rhs);//使⽤⽅式:s1 < s3  
(4) s1 <= s2  
bool operator<= (const string& lhs, const string& rhs);//使⽤⽅式:s1 <= s2  
bool operator<= (const char* lhs, const string& rhs);//使⽤⽅式:s3 <= s1  
bool operator<= (const string& lhs, const char* rhs);//使⽤⽅式:s1 <= s3  
(5) s1 > s2  
bool operator> (const string& lhs, const string& rhs);//使⽤⽅式:s1 > s2  
bool operator> (const char* lhs, const string& rhs);//使⽤⽅式:s3 > s1  
bool operator> (const string& lhs, const char* rhs);//使⽤⽅式:s1 > s3  
(6) s1 >= s2  
bool operator>= (const string& lhs, const string& rhs);//使⽤⽅式:s1 >= s2  
bool operator>= (const char* lhs, const string& rhs);//使⽤⽅式:s3 >= s1  
bool operator>= (const string& lhs, const char* rhs);//使⽤⽅式:s1 >= s3

字符串的⽐较是基于字典序进⾏的,⽐较是对应位置上字符的ASCII值的⼤⼩;⽐较的不是字符串的⻓度。

"abc" < "aq" //'b'的ascii码值是⼩于'q'的  
"abcdef" < "ff" //'a'的ASCII码值是⼩于'f'的  
"100" < "9" //'1'的ASCII码值是⼩于'9'的
#include <iostream>  
#include<string>  
using namespace std;  
int main()  
{  
	string s1 = "hello world";  
	string s2 = "hello";  
	if (s1 == (s2 + " world"))  
	{  
		cout << "s1 == s2" << endl;  
	}  
	else  
	{  
		cout << "s1 != s2" << endl;  
	}  
	
	return 0;  
}
#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string s1 = "abcd";  
	string s2 = "abbcdef";  
	char s3[] = "bbc";
	if (s1 > s2)  
	cout << "s1 > s2" << endl;  
	else  
	cout << "s1 <= s2" << endl;  
	if (s1 == s2)  
	cout << "s1 == s2" << endl;  
	else  
	cout << "s1 != s2" << endl;  
	if (s1 <= s3)  
	cout << "s1 <= s3" << endl;  
	else  
	cout << "s1 > s3" << endl;  
	
	return 0;  
}

和string相关的函数

stoi/stol
  • stoi 是将字符串转换成 int 类型的值
  • stol 是将字符串转换成 long int 类型的值

stoi 函数其实可以将⼀个 string 类型的字符串,转化为整型,函数原型如下

int stoi (const string& str, size_t* idx = 0, int base = 10);  
long stol (const string& str, size_t* idx = 0, int base = 10);
  • str 表⽰被转换的 string 类型的字符串
  • idx 是⼀个输出型参数,也就是这个通过这个参数会带回⼀个值。 idx 是⼀个指针,需要在外边创建⼀个 size_t 类型的值,传递它的地址给 idx ,这个参数将会带回 str 中⽆法正确匹配数字的第⼀个字符的位置。
  • base 表⽰被解析的字符串中数字的进制值,可能是 2 , 8 , 10 , 16 或者 0
    • 默认情况下这个值是 10 ,表⽰ 10 进制数字
    • 如果传递的是 2 ,表⽰被解析的字符串中是 2 进制的数字,最终会转换成 10 进制
    • 如果传递的是 8 ,表⽰被解析的字符串中是 8 进制的数字,最终会转换成 10 进制
    • 如果传递的是 16 ,表⽰被解析的字符串中是 16 进制的数字,最终会转换成 10 进制
    • 如果传递的是 0 ,会根据字符串的内容的信息⾃动推导进制,⽐如:字符串中有 0x ,就认为是 16 进制, 0 开头会被认为是 8 进制,最终会转换成 10 进制。
#include <iostream>  
#include<string>  
using namespace std;  
int main()  
{  
	size_t pos = 0;  
	
	string s1 = "11x34";  
	int ret1 = stoi(s1, &pos, 16);  
	cout << ret1 << endl;  
	cout << "pos:" << pos << endl;  
	
	string s2 = "11x34";  
	int ret2 = stoi(s2, &pos, 2);  
	cout << ret2 << endl;  
	cout << "pos:" << pos << endl;  
	
	string s3 = "0x11x34";  
	int ret3 = stoi(s3, &pos, 0);  
	cout << ret3 << endl;  
	cout << "pos:" << pos << endl;  
	
	return 0;  
}

![[Pasted image 20250306215758.png]]

stod/stof

stod 是将字符串转换成 double 类型的值,函数原型如下,和 stoi 函数的⽐较的话,少了描述字符串中数字进制的参数,其他参数⼀致。 stof 是将字符串转换成 flaot 类型的值。

double stod (const string& str, size_t* idx = 0);  
float stof (const string& str, size_t* idx = 0);
#include <iostream>  
#include<string>  
using namespace std;  
int main()  
{  
	string s = "3.14x456";  
	double ret = stod(s, NULL);  
	cout << ret << endl;  
	
	return 0;  
}

![[Pasted image 20250306220100.png]]

to_string
string to_string (int val);  
string to_string (long val);  
string to_string (long long val);  
string to_string (unsigned val);  
string to_string (unsigned long val);
string to_string (unsigned long long val);  
string to_string (float val);  
string to_string (double val);  
string to_string (long double val);

tostring 函数可以将数字转换成字符串,从上述函数原型的⻆度看的话,可以将整型、浮点型的数字转换成字符串的,使⽤起来也⾮常简单。

#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
	string pi = "pi is " + to_string(3.14159);  
	cout << pi << endl;  
	return 0;  
}
<think>好的,我现在需要处理用户关于C++ string类常用方法总结的查询。首先,用户可能已经看过一些资料,比如他们提到的引用[1]和引用[2],里面提到了operator[]、at()、size()等方法,以及cin和getline的问题。我需要确保覆盖这些内容,同时补充其他常用方法。 首先,我得回忆C++ string类的主要方法。常用的大概包括构造函数、赋值操作、字符串连接、访问字符([]和at)、容量相关如size、length、empty、clear,修改字符串如append、insert、erase、replace,查找如findsubstr,比较操作compare,子串substr,输入输出如getline,还有类型转换如c_str、stoi等。 用户提到的operator[]和at()已经在引用[1]中出现,但需要详细说明它们的区别,比如越界时的行为。另外,size()和length()通常是一样的,但可能用户会混淆,需要指出它们的区别。empty()和clear()也是基础,必须包含。 然后,字符串连接可以用+或append,但需要举例说明。insert和erase在修改字符串时很常用,应该给出例子。replace可能用于替换部分字符串,比如替换特定子串的位置。 查找方法如find和rfind,以及substr提取子串,这些对处理字符串很重要。比较操作compare和直接用运算符的区别可能需要说明。输入输出方面,用户提到了getline和cin的问题,引用[2]中的情况需要解释清楚,避免残留的换行符影响。 类型转换方面,c_str()用于和C字符串交互,stoistol等转换函数也很实用。可能还需要提到C++11引入的stofstod等。 此外,可能需要提醒用户关于字符串的迭代器用法,比如begin()和end(),但可能用户更关注直接的方法而不是STL特性。还有reserve和capacity用于预分配内存,优化性能,但属于进阶内容,是否包含要看用户的需求。 需要检查是否有遗漏的重要方法,比如assign、compare、swap等。assign可以用于赋值,但通常operator=更常用。swap可以交换两个字符串的内容,可能在特定情况下有用。 然后,组织这些内容,按照功能分类,比如构造与赋值、访问元素、容量操作、修改操作、查找与子串、输入输出、类型转换等。每个类别下列举方法,给出示例代码,并注意语法正确性,使用中文注释。 用户可能还关心性能问题,比如operator[]和at()的性能差异,或者append和+=的效率。但如果没有相关资料,可能不需要深入,除非有明确的引用支持。 还需要注意用户提到的引用内容,引用[1]中提到了operator[]和at(),以及size(),引用[2]提到getline和cin的问题,因此在回答中需要适当引用这些内容,比如在讨论getline时引用[2],在讨论字符访问时引用[1]。 最后,生成相关问题,可能涉及字符串操作的其他方法,性能优化,常见错误(如越界),或者与其他数据类型的转换等。比如如何安全访问字符,处理空字符串,避免缓冲区问题等。 总结来说,回答的结构应该是先概述string类,然后分门别类介绍常用方法,每个方法简要说明,示例代码,注意事项,并引用已有资料。最后提出相关问题,帮助用户深入学习。</think># C++ string类常用方法总结 ## 一、构造与赋值 1. **构造函数** ```cpp string s1; // 空字符串 string s2("Hello"); // 从C风格字符串构造 string s3(5, 'A'); // "AAAAA" ``` 2. **赋值操作** ```cpp string s4 = s2; // 拷贝赋值 s4.assign("World", 3); // "Wor" ``` ## 二、元素访问 1. **下标访问** ```cpp char c1 = s2[1]; // 'e'(不检查越界)[^1] char c2 = s2.at(1); // 'e'(越界抛出异常)[^1] ``` ## 三、容量操作 1. **长度与容量** ```cpp int len = s2.size(); // 或 s2.length() bool isEmpty = s1.empty();// 判断是否为空 s2.clear(); // 清空字符串 ``` 2. **预分配内存** ```cpp s2.reserve(100); // 预分配100字节内存 ## 四、修改操作 1. **字符串拼接** ```cpp s2 += " World"; // 运算符重载 s2.append("!!!"); // 追加内容 2. **插入与删除** ```cpp s2.insert(5, " C++"); // "Hello C++ World!!!" s2.erase(5, 4); // 删除第5位开始的4个字符 3. **替换操作** ```cpp s2.replace(6, 5, "STL"); // 替换第6位开始的5个字符为"STL" ## 五、查找与子串 1. **查找方法** ```cpp size_t pos = s2.find("STL"); // 返回首次出现位置 size_t rpos = s2.rfind('o'); // 反向查找 2. **提取子串** ```cpp string sub = s2.substr(6, 3); // 从第6位取3个字符 ## 六、输入输出 1. **安全读取** ```cpp getline(cin, s2); // 读取整行(包括空格)[^2] ``` > 注意:使用`cin >> s2`后接`getline`时,需先用`cin.ignore()`清除残留的换行符[^2] ## 七、类型转换 1. **C风格转换** ```cpp const char* cstr = s2.c_str(); // 获取C风格字符串 2. **数值转换** ```cpp string numStr = "123"; int num = stoi(numStr); // 字符串转整数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值