一、string 概念
string 字符串其实是一种更加高级的封装,string 字符串中包含大量的方法,这些方法使得字符串的操作变得更加简单。string 使用的好,慢慢你就不想使用字符数组来存放字符串了。
而 C++ 中将字符串直接作为一种类型,也就是 string 类型,使用 string 类型创建的对象就是 C++ 的字符串。
string 也就是存储字符串的另一种方式,就是和字符数组差不多,只不过封装了一些好用的功能而已,十分简单易懂
二、string 的常见操作和功能
1、头文件
#include <string>
2、创建字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1;
string s2 = "hello world";
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
return 0;
}
这里就不用多说了,和字符串的初始化一样。
string 也可以下标进行访问。
除了以上创建字符串的写法外,C++ 中还有一些其他的创建字符串方式。如:
当然 C++ 中的 string 创建的字符串和 char 类型的数组所表示的字符串还有一个区别,string 类型的字符串对象可以直接赋值,比如:
方式 1:其他字符串创建写法
string s("hello world");
string s1 = s;
方式 2:string 对象直接赋值示例
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1("hello world");
string s2("hehe");
s2 = s1;
cout << s2 << endl;
return 0;
}
3、string 字符串的输入
(1)正常输入(cin)
和 scanf 功能一样,string 可以用 cin 输入,但是只能输入不带空格的字符串。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
cout << s << endl;
return 0;
}
(2)getline(带空格输入)
- getline 是 C++ 标准库中的一个函数,用于从输入流中读取一行文本,并将其存储为字符串。
- getline 函数有两种不同的形式,分别对应着字符串的结束方式。
istream& getline(istream& is, string& str);
istream& getline(istream& is, string& str, char delim);
✅提示:istream 是输入流类型,cin 是 istream 类型的标准输入流对象。ostream 是输出流类型,cout 是 ostream 类型的标准输出流对象。getline 函数是从输入流中读取一行文本信息,所以如果是在标准输入流(键盘)中读取数据,就可以传 cin 给第一个参数。
第一种(默认以换行符为结束标志)
第一种 getline 函数以换行符('\n')作为字符串的结束标志,它的一般格式是:
getline(cin, string str)
这种形式的 getline 函数从输入流(例如 cin)中读取文本,直到遇到换行符('\n')为止,然后将读取到的文本(不包括换行符)存储到指定的 string 类型的变量 str 中。
实例:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
getline(cin, name);
cout << name << endl;
return 0;
}
第二种(自定义结束标志)
第二种 getline 函数允许用户自定义结束标志,它的一般格式是:
getline(cin, string str, char delim)
这种形式的 getline 函数从输入流中读取文本,直到遇到用户指定的结束标志字符(delim)为止,然后将读取到的文本(不包括结束标志字符)存储到指定的 string 类型的变量 str 中。
实例:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
getline(cin, name, 'q');
cout << name << endl;
return 0;
}
4、size()——字符串长度
string 中提供了 size() 函数用于获取字符串长度。
在 C++ 中关于字符串的操作函数都是包含在 string 中的,所以需要调用这些函数时,通常用 .点运算符。
使用示例:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
string s1 = "hello";
string s2 = "hello world";
string s3 = "12ab!~ ";
cout << "s:" << s.size() << endl;
cout << "s1:" << s1.size() << endl;
cout << "s2:" << s2.size() << endl;
cout << "s3:" << s3.size() << endl;
return 0;
}
5、迭代器(iterator)
迭代器是一种对象,它可以用来遍历容器(比如我们现在学习的 string)中的元素,迭代器的作用类似于指针,或者数组下标。
❗ 不过访问迭代器指向的值,需要解引用 (*)。
C++ 中的 string 提供了多种迭代器,用于遍历和操作字符串中的内容。这里给大家介绍一种最常用的迭代器。
begin() 和 end()
- begin():返回指向字符串第一个字符的迭代器,需要一个迭代器的变量来接收。
- end():返回指向字符串最后一个字符的下一个位置的迭代器(该位置不属于字符串)。
- string 中 begin() 和 end() 返回的迭代器的类型是 string::iterator(也可以用 auto 来接收)。
(1)比较
✅注意迭代器是可以进行大小比较,也可以进行 + 或者 - 整数运算的。
比如:i++,就是让迭代器前进一步,i-- 就是让迭代器退后一步。同一个容器的两个迭代器也可以相减,相减结果的绝对值,是两个迭代器中间元素的个数。
代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "abcdef";
string::iterator it1 = s.begin();
string::iterator it2 = s.end();
cout << (it1 < it2) << endl;
cout << it1 - it2 << endl;
return 0;
}
(2)遍历
迭代器通常用于遍历字符串的,可以正序遍历,也可以逆序遍历
正序遍历如下
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "abcdef";
for (auto it = s.begin(); it != s.end(); ++it) {
cout << *it << ' ';
}
for (string::iterator it = s.begin(); it != s.end(); ++it) {
cout << *it << ' ';
}
return 0;
}
改变指定字符
通过迭代器找到元素后,改变迭代器指向的元素,是可以直接改变字符串内容的。
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "abcdef";
cout << str << endl;
for (string::iterator it = str.begin(); it != str.end(); ++it) {
*it = 'x';
}
cout << str << endl;
return 0;
}
6、字符串的插入和删除
(1)插入
push_back()——尾插
push_back() 用于在字符串尾部插一个字符。
具体实现如下
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
s.push_back('h');
s.push_back('e');
s.push_back('l');
s.push_back('l');
s.push_back('o');
cout << s << endl;
string s1 = "hello ";
s1.push_back('w');
s1.push_back('o');
s1.push_back('r');
s1.push_back('l');
s1.push_back('d');
cout << s1 << endl;
string s2;
for (char c = 'a'; c <= 'f'; c++) {
s2.push_back(c);
}
cout << s2 << endl;
return 0;
}
字符串的 += 和 + 运算
push_back() 用于在字符串后添加单个字符,若需向字符串后追加字符串,string 类型支持 + 和 += 运算,本质是 string 中重载了 operator+= 操作符。
代码举例
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello";
s += " world";
cout << s << endl;
string s2 = "hello";
s2 = "world " + s2;
cout << s2 << endl;
return 0;
}
insert——指定位置进行插入
如果我们需要在字符串中间的某个位置插入一个字符串,怎么办呢?这时候我们得掌握一个函数就是 insert
string& insert(size_t pos, const string& str);
string& insert(size_t pos, const char* s);
string& insert(size_t pos, size_t n, char c);
pos 就是字符串下标
流程如下
代码功能实现:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "abcdefghi";
string base = s;
cout << "原始字符串:" << s << endl;
string str = "xxx";
s.insert(3, str);
cout << "方式 1-插入 string 字符串:" << s << endl;
s = base;
s.insert(3, "xxx");
cout << "方式 2-插入 C 风格字符串:" << s << endl;
s = base;
s.insert(3, 3, 'x');
cout << "方式 3-插入 3 个字符 x:" << s << endl;
return 0;
}
(2)删除——pop_back()
pop_back() 用于删除字符串中尾部的一个字符。
实现
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello";
cout << "s:" << s << endl;
s.pop_back();
cout << "s:" << s << endl;
s.pop_back();
cout << "s:" << s << endl;
return 0;
}
注意:不可对空字符串继续进⾏ pop_back() 操作,否则程序出现异常
7、字符串的查找和截取
(1)查找——find()
find() 函数用于查找字符串中指定子串/字符,并返回子串/字符在字符串中第一次出现的位置。
find 类型
size_t find(const string& str, size_t pos = 0) const;
size_t find(const char* s, size_t pos = 0) const;
size_t find(const char* s, size_t pos, size_t n) const;
size_t find(char c, size_t pos = 0) const;
返回值
- 若找到,返回子串/字符在字符串中第一次出现的起始下标位置。
- 若未找到,返回一个整数值
npos(针对 npos 的介绍会在下面给出)。通常判断 find() 函数的返回值是否等于 npos 就能知道是否查找到子串或者字符。
不同类型 find 例子
代码例子
代码 1——查找【字符串 / 子串】
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world hello everyone";
string str = "llo";
size_t n = s.find(str);
cout << n << endl;
n = s.find(str, n + 1);
cout << n << endl;
n = s.find("llo");
cout << n << endl;
n = s.find("llo", n + 1);
cout << n << endl;
return 0;
}
代码 2——精准查找【C 风格字符串的前 N 个字符】
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world hello everyone";
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 中定义的一个静态常量。我们通常会通过判断 find 函数的返回值是否等于 npos,来确定查找是否成功。
#include <iostream>
#include <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;
}
(2)截取——substr()
函数参数
substr() 函数用于截取字符串中指定位置指定长度的子串。函数原型如下:
string substr(size_t pos = 0, size_t len = npos) const;
substr() 的用法:
- 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 everyone";
string s1 = s.substr(7);
cout << s1 << endl;
string s2 = s.substr(7, 6);
cout << s2 << endl;
return 0;
}
(3)find() 和 substr() 结合
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;
}
8、string 的关系运算
string 支持字符串比较,和 strcmp 比较规则一致,可以直接进行字符串比较,比如 >,=,<等等,只不过返回类型为 bool 类型
这里仅简单展示
#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;
}
9、string 中字符串与基本数值类型的转换工具函数
(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:输出型参数(指针类型)。需在外创建
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;
}
(2)stod/stof
stod 是将字符串转换成 double 类型的值,就是参数少了进制的描述
参数
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;
}
(3)to_string
就是将数字转换为字符串
string to_string(数字类型 val);
代码举例
#include <iostream>
#include <string>
using namespace std;
int main() {
string pi = "pi is " + to_string(3.14159);
cout << pi << endl;
return 0;
}