C++顺序容器之二:string
前导知识
在之前我们分享过顺序容器的一些知识,std::string也支持顺序容器共同的操作,这里就不再赘述,详情可以点击下面的链接查看。
https://bg.littleao.top/archives/cpp_sequence_containers
除此之外,标准库string类型定义了大量函数。以便我们轻松的对字符串进行操作。
构造string
除开已经介绍过的构造函数,string类型还支持另外三个构造函数,如下所示:
string s(cp, n):s是cp指向的数组中前n个字符的拷贝,此数组至少包含n个字符。string s(s2, pos2):s是string s2从下标pos2开始得到字符的拷贝。若pos2 > s2.size(),构造函数的行为未定义。string s(s2, pos2, len2):s是string s2从下标pos2开始len2个字符的拷贝。若pos2 > s2.size(),函数行为未定义。构造函数至多拷贝s2.size()-pos2个字符,不管len2的值是多少。
第一个构造函数能够接受const char*类型和string类型,接受指定拷贝多少个字符的参数。如果拷贝范围超过了存在的范围,会抛出out_of_range异常。
const char* cp = "This is an example sentence.";
std::string s1(cp); // This is an example sentence.
std::string s2(cp, 8); // This is
std::string s3(cp+8); // an example sentence.
std::string s4(s1, 8); // an example sentence.
std::string s5(s1, 8, 10); // an example
string的方法
substr
s.substr(pos, n):返回一个string,包含从s中从pos开始n个字符的拷贝,n有默认值0。超出范围报out_of_range错误。
string s("hello world");
string s2 = s.substr(6); // world
string s3 = s.substr(0, 5); // hello
额外的insert、assign和erase功能
string不仅支持顺序容器中insert,assign和erase功能,该提供了额外版本,简单来说就是之前支持迭代器的部分现在支持了直接下标操作。
std::string s("hello, world");
s.insert(s.size(), 5, '!'); // 在字符串后面添加5个!
s.insert(s.end(), 5, '!'); // 迭代器操作,和上面一样的效果
s.erase(s.size() - 5, 5); // 移除5个!
assign可以接受一个c风格字符串进行赋值。
append和replace
append会在字符串末尾添加字符,replace会将特定字符串替换,使用方法如下:
std::string s("hello, world");
s.append(" and welcome."); // 后接字符串
s.replace(7, 5, "people"); // 将第7个字符往后5个字符删除,并插入people
搜索
string类型提供了6种不同的搜索方式,具体如下:
s.find(ss):最简单的搜索,找到返回第一个匹配的下标,找不到则返回string::npos。s.find_first_of(ss),在字符串搜索ss的任意一个字符,返回第一个匹配的下标,不匹配返回npos。s.find_first_not_of(ss),在字符串搜索不存在于ss的第一个字符,返回同上。s.rfind(ss):find的逆序,返回最后一个匹配的下标。s.find_last_of(ss):find_first_of的逆序,返回最后一个符合的下标。s.find_last_not_of(ss):find_first_not_of的逆序,返回最后一个符合的下标。
这些函数接收的参数有以下几种:
c, pos=0:查找字符c, 从pos开始。ss, pos=0:查找字符串ss,从pos开始。cp, pos=0:查找字符串cp,从pos开始。cp, pos, n:从pos查找后n个匹配的字符串。
std::string s("This is my email: john117@outbook.com");
auto pos1 = s.find("is"); // 2
auto pos2 = s.find_first_of("0123456789"); // 22
auto pos3 = s.rfind("is"); // 5
auto pos4 = s.find_first_not_of("0123456789"); // 0
auto pos5 = s.find_last_not_of("0123456789", 5, 5); // 5
auto pos6 = s.find_last_of("0123456789"); // 24
compare
std::string 类中的 compare 函数用于比较两个字符串的大小关系。这个函数可以用于按字典顺序比较两个字符串,返回一个整数值来表示比较结果。
参数可以是c风格类型字符串,也可以是string类型字符串。可以指定比较的范围:
std::string s1("apple");
std::string s2("pineapple");
auto a = s1.compare(s2); // 1
auto b = s2.compare(s1); // -1
auto c = s2.compare(4, 5, s1); // 0
可以用关系运算符来比较。
数值转换
在 C++ 中,将 std::string 和数值类型(如整数、浮点数)相互转换是一种常见的操作。以下是一些常用的方法:
- 将字符串转换为数值
使用std::stoi,std::stol,std::stoll,std::stoul,std::stoull,std::stof,std::stod,std::stold等函数:
std::string str = "123";
int num = std::stoi(str); // 将字符串转换为整数
long num_long = std::stol(str); // 将字符串转换为长整数
float num_float = std::stof(str); // 将字符串转换为单精度浮点数
double num_double = std::stod(str); // 将字符串转换为双精度浮点数
- 将数值转换为字符串
使用std::to_string函数:
int num = 123;
std::string str = std::to_string(num); // 将整数转换为字符串
评论区