string常见操作(下)
文章目录
9. find()
find() 函数用于查找字符串中指定子串/字符,并返回子串/字符在字符串中第一次出现的位置。

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>
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;
}
在字符串中查找字符或者字符串时,有可能找不到,这时候find 函数会范围npos 这个值,该数字并不是一个随机的数字,而是string 中定义的一个静态常量npos 。我们通常会判断find 函数的返回值是否等于npos 来判断,查找是否成功。
static const size_t npos = -1;
打印出来看看:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//注意:npos是string中定义的,使用npos需要带上string::指明是string类
cout << "npos" << string::npos << endl;
return 0;
}

10. substr()
substr() 函数用于截取字符串中指定位置指定长度的子串。函数原型如下:
string substr (size_t pos = 0, size_t len = npos) const;
参数说明:
pos:子串的起始位置(从0开始计数)。len:要截取的子串长度。如果省略或超过剩余长度,则截取到字符串末尾。
substr():如果函数不传参数,就是从下标为0的位置开始截取,直到结尾,得到的是整个字符串;
substr(pos):从指定下标pos 位置开始截取子串,直到结尾;
substr(pos, len) :从指定下标pos 位置开始截取长度为len 的子串。
返回值类型:string ,返回的是截取道德字符串,可以使用string类型的字符串接收。
示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello world hello everynoe";
string s1 = s.substr(7);
cout << s1 << endl;
string s2 = s.substr(7, 6);
cout << s2 << endl;
return 0;
}

substr() 和find() 经常配合使用,find() 负责找到位置,substr() 从这个位置向后获得字符串。
示例:
#include <iostream>
#include <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;
}

11. string的关系运算
在实际写代码的过程中经常会涉及到两个字符串大小的比较,比如:判断你输入的密码是否正确,将输入夫人密码和数据库中正确的密码比较。
那么两个string 类型的字符串是否可以比较大小呢?其实是可以的,C++中为string提供了一系列的关系运算。
11.1 支持的关系运算
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
注:关于操作符的重载,只有深入学习C++的类和对象,才能深入理解和掌握,在竞赛中不需要深入挖掘,会使用就行,但要用C++进行工程性开发时这部分知识一定要补上。
字符串的比较是基于字典序进行的,比较是对应位置上字符的ASCII值的大小;比较的不是字符串的长度。
比如:
"abc" < "aq" //'b'的ASCII码值是小于'q'的
"abcdef" < "ff" //'a'的ASCII码值是小于'f'的
"100" < "g" //'1"的ASCII码值是小于'9'的
11.2 代码举例
示例1:
#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;
}
示例2:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "abcd";
string s2 = "abcdef";
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;
}
12. 和string相关的函数
12.1 stoi/stol
stoi是将字符串转换成int类型的值stol是将字符串转换成long int类型的值
这两个函数非常类似,这里以stoi 为例讲解一下这里函数的使用方式。
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进制。
- 默认情况下这个值是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;
}

12.2 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;
}

12.3 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);
to_string 函数可以将数字转换成字符串,从上述函数原型的角度看的话,可以将整型、浮点型的数字转换成字符串,使用起来也很简单。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string pi = "pi is " + to_string(3.14159);
cout << pi << endl;
return 0;
}
5234

被折叠的 条评论
为什么被折叠?



