目录
一、关于string类:
在C语言中对于字符串的操作是比较麻烦的,所以在C++中就封装了一个string来进行简化对于字符串的操作。
二、什么是string:
1、string是表示字符串的字符串类。
2、该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
3、不能操作多字节或者变长字符的序列。
以下是string中的成员对象:
a为指向该字符串的指针
_capacity为总空间
_size为字符个数
class string
{
private:
char* a;
int _capacity;
int _size;
};
三、string中成员函数的用法:
1、常见构造:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
string s2("autumn");
string s3(5, 'x');
string s4(s2);
cout << s1 << endl << s2 << endl << s3 << endl << s4 << endl;
return 0;
}
以上s1~s4依次对应上面的各个。
2、string类对象的容量操作:
以下是对上述的函数进行使用:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("abcdefg");
cout << "s1.size() = " << s1.size() << endl;//s1的字符串长度
cout << "s1.length() = " << s1.length() << endl;//s1的字符长度
cout << "capacity() = " << s1.capacity() << endl;//返回该字符串所占空间总大小,如果总空间不够会进行扩容,根据不同的编译器进行不同的扩容大小
s1 += "aaaaaaaaaaaaaa";
cout << "扩容后:capacity() = " << s1.capacity() << endl;
cout << "s1.empty() = " << s1.empty() << endl;//是空返回1,不是空返回0
s1.clear();
cout << "s1.clear() 后: " << s1 << endl;
s1.reserve(100);
cout << "s1.capacity() = " << s1.capacity() << endl;
string s2("aaaaaaaaa");
s2.resize(10, 'x');
cout << "size:" << s2.size() << endl;
cout << "s1:" << s2 << endl;
return 0;
}
运行结果:
在上述中,size()函数除了跟length()函数一样可以获取字符串长度之外,还可以获取vector类型的长度。
3、string类的访问和遍历操作:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("abcdef");
cout << s1[0] << endl;
cout << s1[5] << endl;
s1[2] = 'x';
cout << "正向迭代器遍历:" << endl;
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
it++;
}
cout << endl;
cout << "反向迭代器遍历:" << endl;
string::reverse_iterator rit = s1.rbegin();
while (rit != s1.rend())
{
cout << *rit << " ";
rit++;
}
cout << endl;
cout << "范围for遍历:" << endl;
for (auto x : s1)
{
cout << x << " ";
}
cout << endl;
return 0;
}
4、 string类对象的修改操作:
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout << "原字符串:" << endl;
string s1("abcdefg");
cout << s1 << endl;
cout << "push_back后:" << endl;
s1.push_back('h');
cout << s1 << endl;
cout << "append后:" << endl;
s1.append("hijklmn");
cout << s1 << endl;
cout << "+=后:" << endl;
s1 += "opq123";
cout << s1 << endl;
cout << "c_str:" << endl;
const char* m = s1.c_str();
cout << m << endl;
cout << "find:" << endl;
int npos1 = s1.find('f');
cout << npos1 << endl;
cout << "rfind:" << endl;
int npos2 = s1.rfind('n');
cout << npos2 << endl;
cout << "substr后:" << endl;
string num = s1.substr(3, 8);
cout << num << endl;
return 0;
}
5、常用的非成员函数:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello ");
string s2("ppr");
//operator+涉及深层拷贝,不建议多用
cout << "operator+后:";
cout << operator+(s1, s2) << endl;
cout << "operator>>:";
string s3;
operator>>(cin, s3);
cout << s3 << endl;
cout << "operator<<:";
operator<<(cout, s2) << endl;
//正常读取是读取不到空格或者回车后面字符串的(此时空格或者回车后面的字符串就在缓冲区域中),但是用getline就可以全部读取一整行字符串
cout << "getline:";
string s4;
getline(cin, s4);
cout << s4 << endl;
return 0;
}
如上cin后输入www wwww wwwww
这样第一次读取是不能够读取空格或者回车后面的字符串的,
在下面中getline就可以全部读取。