在使用string类时,必须包含#include头文件以及using namespace std。
npos是一个静态的全局变量为-1补码无穷大
string类的常用接口说明
1.string类对象的常见构造![](https://i-blog.csdnimg.cn/blog_migrate/e466a0458bcc151d732c7d234ed3957e.png)
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
#include<string>
int main()
{
//4个构造函数
string s1;
string s2("hello");
string s3(s2);
string s4(5, 'a');
string s5 = "hello";
string s6 = s2;
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
//析构函数先释放s4,再s3,再s2,再s1
cout << s5 << endl;
cout << s6 << endl;
s1 = s2;
cout << s1 << endl;
return 0;
}
void TestString1() //构造函数
{
string s1; //ok
string s2("bit"); //ok
string s3(s2); //ok
string s4("bit", 1);
string s5("bit education", 1, 2);
string s6("bit education", 1, string::npos); //npos是一个静态的全局变量为-1补码无穷大
string s7("bit education", 1,20);
string s8(5, 'a');
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
cout << s6 << endl;
cout << s7 << endl;
cout << s8 << endl;
s1 = s8; //赋值 ok
cout << s1 << endl;
}
2.string类对象的容量操作
![](https://i-blog.csdnimg.cn/blog_migrate/ee2d7e2f901b68f3d399ee28fd32249b.png)
void TestString7() //string类的容量操作
{
string s1("bit education");
string s2("C++");
cout << s1.size() << endl;
cout << s2.size() << endl;
cout << s1.length() << endl;
cout << s2.length() << endl;
cout << s1.max_size() << endl;
cout << s2.max_size() << endl;
cout << s1.capacity() << endl;
cout << s2.capacity() << endl;
s1.clear(); //size减为0
cout << s1 << endl;
cout << s1.capacity() << endl;
}
3.string类对象的访问及遍历
![](https://i-blog.csdnimg.cn/blog_migrate/399bcbef88a58a5bbaecddccb6f7ea86.png)
void TestString2() //[]+下标的遍历,重载了+=运算符----追加
{
string s1("bit");
s1 += 'e';
cout << s1 << endl;
s1 += ' ';
s1 += "ducation";
cout << s1 << endl;
//[]+下标的遍历方式
for (size_t i = 0; i < s1.size(); i++) //读
{
cout << s1[i] << " ";
}
cout << endl;
for (size_t i = 0; i < s1.size(); i++) //写,修改字符
{
s1[i] = s1[i] + 1;
}
for (size_t i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
}
迭代器的遍历
void TestString3() //迭代器的遍历
{
string s1("bit education");
string::iterator it = s1.begin();
//auto it=s1.begin();auto自动推导it是迭代器
//vector<int>::iterator it=s1.begin();
//读
while (it != s1.end())
{
cout << *it << " ";
it++;
}
cout << endl;
//写
it = s1.begin();
while (it != s1.end())
{
*it = *it + 2;
it++;
}
cout << s1 << endl;
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
vector<int>::iterator vit = v1.begin();
while (vit != v1.end())
{
cout << *vit << " ";
vit++;
}
cout << endl;
}
void TestString4() //范围for的遍历 C++11原理是迭代器
{
string s1("bit education");
for (auto s : s1) //依次取出容器s1里面的字符给s
{
cout << s << " ";
}
cout << endl;
for (auto s : s1)
{
s = s + 1;
cout << s << " ";
}
cout << endl;
}
![](https://i-blog.csdnimg.cn/blog_migrate/c24162c0482969648f5322a546e013b4.png)
反向迭代器,倒着遍历
void TestString5() //反向迭代器,倒着遍历
{
string s1("bit education");
string::reverse_iterator rit = s1.rbegin();
while (rit != s1.rend())
{
cout << *rit << " ";
rit++;
}
cout << endl;
}
4. string类对象的修改操作
![](https://i-blog.csdnimg.cn/blog_migrate/eb995f36a7f23837b0b91b810b861c2b.png)
void TestString11() //string 类对象的修改操作
{
string s("bite");
cout << s<< endl;
s.push_back(' ');
cout << s << endl;
s.append("education");
cout << s << endl;
s += "!";
cout << s << endl;
s.insert(s.begin(), 'B');
cout << s << endl;
s.insert(0, "A");
cout << s << endl;
s.erase(2, 3);
cout << s << endl;
s.erase(s.begin(),s.end());
cout << s << endl;
}
c_str
转换后遇到\0就会结束,string则会全部输出
rfind和substr
void TestString13() //find获取文件的后缀,rfind可以倒着找
{
string s1("string.cpp.zip");
string s2("string.c");
string s3("string.txt");
size_t pos1 = s1.find('.');
if (pos1 != string::npos)
{
cout << s1.substr(pos1) << endl;
}
size_t pos2 = s2.find('.');
if (pos2 != string::npos)
{
cout << s2.substr(pos2) << endl;
}
size_t pos3= s3.find('.');
if (pos3 != string::npos)
{
cout << s3.substr(pos3) << endl;
}
//协议 域名 资源名称
string stringcpp("http://www.cplusplus.com/reference/string/string/find/");
string &url = stringcpp;
size_t pos4 = url.find(":");
if (pos4 != string::npos)
{
cout << url.substr(0,pos4) << endl;
}
size_t pos5 = url.find('/', pos4 + 3);
if (pos5 != string::npos)
{
cout << url.substr(pos4 + 3, pos5 - pos4-3) << endl;
}
cout << url.substr(pos5 + 1) << endl;
}
5. string类非成员函数
![](https://i-blog.csdnimg.cn/blog_migrate/3febaaaebfa32907a46850b8512b51c9.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9ca0e493a8b19c983a71f2217fddd0e9.png)