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
#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() 函数⽤于查找字符串中指定⼦串/字符,并返回⼦串/字符在字符串中第⼀次出现的位置
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;
}
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 的⼦串
返回值类型: 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;
}
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;
}
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;
}
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;
}
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;
}