string类
1、string是表示字符序列的类
2、标准的string类提供了对此类对象的支持,并添加了专门用来操作单字节字符的设计特性
3、string类是使用char,使用它的默认char_traits和分配器类型
4、string类是basic_string模板类的一个实例 typedef basic_string<char, char_traits, allocator> string;
5、这个类不能操作多字节或者变长字符的序列,例如:若是处理多字节或变长字符的序列,这个类的所有成员以及它的迭代器任然按照字节来操作。
1、string类对象的常见构造
(constructor)函数名称 | 功能说明 |
---|
string() | 构造空的string类对象,即空字符串 |
string(const char* s) | 用C-string来构造string类对象 |
string(size_t n, char c) | string类对象中包含n个字符c |
string(const string&s) | 拷贝构造函数 |
#include <iostream>
#include <string>
using namespace std;
void TestString1()
{
string s1;//构造空的string类对象s1
string s2("Hello, World!");//c格式构造string类类型s2
string s3(s2);//拷贝构造s3
string s4(10, 'a');//构造10个字符'a'字符
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
}
int main()
{
TestString1();
system("pause");
return 0;
}
2、string类对象的容量操作
函数名称 | 功能说明 |
---|
size | 返回字符串有效字符长度 |
length | 返回字符串有效字符长度 |
capacity | 返回空间总大小 |
empty | 检测字符串释放为空串,是返回true,否则返回false |
clear | 清空有效字符 |
reserve | 为字符串预留空间 |
resize | 将有效字符的个数该成n个,多出的空间用字符c填充 |
#include <iostream>
#include <string>
using namespace std;
void TestString2()
{
string s("Hello,World!");
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.clear();
cout << s.size() << endl;
cout << s.capacity() << endl;
s.resize(10, 'a');
cout << s.size() << endl;
cout << s.capacity() << endl;
s.resize(15);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(5);
cout << s.size() << endl;
cout << s.capacity() << endl;
//resize不会缩小原来已经扩充的capacity
}
void TestString3()
{
string s;
s.reserve(100);
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(50);
cout << s.size() << endl;
cout << s.capacity() << endl;
//reserve不会缩小已经扩充的capacity
}
void TestString4()
{
string s;
size_t size = s.capacity();
cout << "making s grow:" << endl;
for (int i = 0; i < 100; i++)
{
s.push_back('c');
if (size != s.capacity())
{
size = s.capacity();
cout << "capacity changed: " << size << endl;
}
}
}
int main()
{
TestString2();
TestString3();
TestString4();
system("pause");
return 0;
}
注:
1、size()和length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致;
2、clear只会清空string中的有效字符,不会改变底层空间大小
3、resize(size_t n)与resize(size_t n, char c)都是将字符串中的有效字符个数改变到n个,resize(size_t n)是将多出来的空间补0,resize(size_t n, char c)是将多出来的字符补c
4、resize不会缩小原来已经扩充的capacity
5、reserve(size_t res_arg = 0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间大小时,reserve不会改变容量大小
6、任何操作不会导致空间缩小
3、string类对象的访问及遍历操作
函数名称 | 功能说明 |
---|
operator[] | 返回pos位置的字符,const string类对象调用 |
begin+ end | begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器 |
rbegin + rend begin | 获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器 |
范围for | C++11支持更简洁的范围for的新遍历方式 |
#include <iostream>
#include <string>
using namespace std;
void TestString1()
{
string s1("Hello, World!");
const string s2("NI,HAO!");
cout << s1 << endl;
cout << s2 << endl;
cout << s1[0] << endl;
cout << s2[0] << endl;
}
void TestString2()
{
string s("Hello,World!");
//for + operator[]遍历
for (int i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
//迭代器遍历
string::iterator it = s.begin();
for (; it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//范围for遍历
for (auto& e : s)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
TestString1();
TestString2();
system("pause");
return 0;
}
4、string类对象的修改操作
函数名称 | 功能说明 |
---|
push_back | 在字符串后尾插字符c |
append | 在字符串后追加一个字符串 |
operator+= | 在字符串后追加字符串str |
c_str | 返回C格式字符串 |
find + npos | 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置 |
rfind | 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置 |
substr | 在str中从pos位置开始,截取n个字符,然后将其返回 |
#include <iostream>
#include <string>
using namespace std;
void teststring1()
{
string s;
s.push_back(' ');//尾插
s.append("hello");//追加字符串
s += ',';//追加字符
s += "world!";//追加字符串
cout << s << endl;
cout << s.c_str() << endl;//以c语言的方式打印字符串
//获取file的后缀
string file("string.cpp");
size_t pos = file.rfind(".");
string suffix(file.substr(pos, file.size() - pos));
cout << suffix << endl;
//取出url中的域名
string url("http://www.cplusplus.com/reference/string/string/find/");
cout << url << endl;
size_t start = url.find("://");
if (start == string::npos)
{
cout << "invalid url" << endl;
return;
}
start += 3;
size_t finish = url.find('/', start);
string address = url.substr(start, finish - start);
cout << address << endl;
//删除url中的协议前缀
pos = url.find("://");
url.erase(0, pos + 3);
cout << url << endl;
}
int main()
{
teststring1();
system("pause");
return 0;
}
注:
1、在string尾部追加字符时,s.push_back©/s.append(1, c)/s += 'c’三种方式实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,也可连接字符串
2、对string操作时,如果能够大概预估到多少个字符,可以先通过reverse把空间预留好
5、string类成员函数
函数 | 功能说明 |
---|
operator+ | 尽量少用,因为传值返回,导致深拷贝效率低 |
operator>> | 输入运算符重载 |
operator<< | 输出运算符重载 |
getline | 获取一行字符串 |
relational operators | 大小比较 |