目录
1.简介
1.1 什么是STL
1.2 STL六大组件
2.string概念
3.string常用接口说明
3.1 string构造
1.直接创建:string();
2.创建并插入字符串:string (const char* s);
3.创建与其他字符串相同的新字符串:string (const string& str);
4.创建含n个相同的字符的字符串:string (size_t n, char c);
5.创建一个含指定字符串前n个字符的新字符串:string (const char* s, size_t n);
6.创建含一个字符串指定位置开始前n个字符的新字符串:string (const string& str, size_t pos, size_t len = npos);(最后一个参数不写默认把该位置后面全部字符复制)
3.2 string容量操作
1.size&&length:返回字符串有效字符长度
2.capacity:返回空间总大小
3.empty:检测字符串释放为空串,是返回true,否则返回false
4.clear:清空有效字符
string s("12345");
cout << s << endl;
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s.empty() << endl<<endl;
s.clear();
cout << s << endl;
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s.empty() << endl<<endl;
输出结果:
5.reserve:为字符串预留空间
string s("12345");
cout << s << endl;
cout << s.size() << endl;
cout << s.capacity() << endl<<endl;
//1.reserve参数<=string的底层空间总大小(即capacity)时,reserver不会改变容量大小。
s.reserve(14);
cout << s << endl;
cout << s.size() << endl;
cout << s.capacity() << endl << endl;
//2.reserve参数>string的空间大小,reserve会进行相应地扩容
s.reserve(20);
cout << s << endl;
cout << s.size() << endl;
cout << s.capacity() << endl << endl;
输出结果:
6.resize:将有效字符的个数该成n个,多出的空间用字符c填充
//resize 重新设定string大小(size)
//这里分三种情况:1.resize值<=size;2.resize值>size&&resize值<=capacity;3.resize值>capacity
//1.resize参数<=size
string s1("hhcyyds");
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.resize(3);
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl << endl;
//2.resize参数>size&&resize参数<=capacity
string s2("hhcyyds");
cout << s2 << endl;
cout << s2.size() << endl;
cout << s2.capacity() << endl;
s2.resize(14,'6');
cout << s2 << endl;
cout << s2.size() << endl;
cout << s2.capacity() << endl<<endl;
//3.resize参数>capacity
string s3("hhcyyds");
cout << s3 << endl;
cout << s3.size() << endl;
cout << s3.capacity() << endl;
s3.resize(20, '6');
cout << s3 << endl;
cout << s3.size() << endl;
cout << s3.capacity() << endl << endl;
输出结果:
3.3 string访问及遍历操作
1.operator[]重载遍历:返回pos位置的字符,const string类对象调用
2.迭代器遍历:begin(rbegin)获取一个字符的迭代器 + end(rend)获取最后一个字符下一个位置的迭代器
3.范围for遍历:C++11支持更简洁的范围for的新遍历方式
string s1("12345");
cout << s1 << endl;
//1.下标遍历
for (int i = 0; i < s1.size(); i++)
{
s1[i] += 1;
}
cout << s1 << endl;
//2.范围for遍历
for (auto& t: s1)
{
t++;
}
cout << s1 << endl;
//3.迭代器遍历
string::iterator t = s1.begin();
while (t != s1.end())
{
*t += 1;
t++;
}
cout << s1 << endl;
string::reverse_iterator rt = s1.rbegin();
while (rt != s1.rend())
{
cout << *rt;
rt++;
}
输出结果:
3.4 stirng修改操作
string s("hhc");
cout << s << endl;
s.append("yyds");
cout << s << endl;
s.push_back('1');
cout << s << endl;
s += "666";
cout << s << endl;
输出结果:
string s("hhcyyds1");
cout << s.c_str() << endl;
![](https://img-blog.csdnimg.cn/80c66727724a4037ba94a6e3bb60c4a4.png)
string s("hhchhc");
cout << s.find('c') << endl;
cout << s.rfind('c') << endl;
cout << s.find('c',3) << endl;
cout << s.rfind('c',3) << endl;
![](https://img-blog.csdnimg.cn/01d4e86843774dd8a8aa86b259a2277c.png)
string s("hhcyyds1");
cout << s.substr(3, 4) << endl;
cout << s.substr(3) << endl;
输出结果: