string类
标准库中的string类
string类
- 字符串是表示字符序列的类
- 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。
- string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型
- string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits和allocator作为basic_string的默认参数
- 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。
注:
string类用utf-8编码,按单字节处理
u16string用utf-16编码
u32string用utf-32编码
wstring类按两字节处理
编码:
计算机中存储只有二进制0、1,用对应的ASCII表来表示文字(支持英文的)其中ASCII表是对256个值建立一个对应的表示值
在早期只有欧美国家使用计算机(早期的计算机中只能表示英文,不能表示其他国家的文字),后来全世界各个国家都开始用计算机了,需要建立自己的编码表
在Linux中常用utf-8、utf-16、utf-32
在Windows中常用gbk
总结:
- string是表示字符串的字符串类
- 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
- string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator> string;
- 不能操作多字节或者变长字符的序列。
在使用string类时,必须包含#include < string > 头文件以及using namespace std;
string类的常用接口说明
具体用法见string类的常见接口说明
- string类对象的常见构造
- string类对象的访问及遍历操作
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
string s2("hello");
const string s3("hehe");
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
// 1. for+operator[] 遍历+修改
for (size_t i = 0;i < s2.size();i++)
{
s2[i] += 1;// 修改
}
for (size_t i = 0;i < s2.size();i++)
{
cout << s2[i] << ""; // 遍历
}
cout << endl;
// 2. 范围for 遍历+修改
for (auto& ch : s2)
{
ch -= 1;// 修改
}
for (auto ch : s2)
{
cout << ch << "";// 遍历
}
cout << endl;
// 3. 迭代器 遍历+修改
string::iterator it = s2.begin();
while (it != s2.end())
{
(*it)++;// 修改
it++;
}
it = s2.begin();
while (it != s2.end())
{
cout << *it << "";// 遍历
it++;
}
cout << endl;
//反着遍历对象
string::reverse_iterator rit = s2.rbegin();
while (rit != s2.rend())
{
cout << *rit << "";
rit++;
}
cout << endl;
//const 对象的遍历
string::const_iterator cit = s3.begin();
while (cit != s3.end())
{
cout << *cit << "";
cit++;
}
cout << endl;
//const 对象反着的遍历
string::const_reverse_iterator rcit = s3.rbegin();
while (rcit != s3.rend())
{
cout << *rcit << "";
rcit++;
}
return 0;
}
总结:
迭代器是一个行为像指针的东西,有可能是指针,也有可能不是指针
迭代器可以用统一类似的方式去访问修改容器
begin()返回的是第一个有效数据位置的迭代器,end()返回的是最后一个有效数据的下一个位置的迭代器
rbegin()返回的是最后个有效数据位置的迭代器,rend()返回的是第一个有效数据的前一个位置的迭代器
所有的容器都支持用迭代器,所以迭代器才是容器通用的访问方式(vector/string这样的结构支持下标+[]去访问,而像list、map这样的就不支持了)
const对象要用const迭代器,只读,不能写
operator[]和at的区别:operator[]如果发生越界访问会报断言错误(assert),而at会报异常(需要捕获异常)
- string类对象的容量操作
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello");
//推荐用size()
cout << s1.size() << endl;
cout << s1.length() << endl;
cout << s1.capacity() << endl;
cout << s1.max_size() << endl;//max_size()实际中没有意义
string s2("hello world");
cout << s2 << endl;
cout << s2.size() << endl;
//将s2中有效字符个数增加到20个,多出位置用缺省值'\0'进行填充
// 注意此时s2中有效字符个数已经增加到20个
s2.resize(20);
cout << s2 << endl;
cout << s2.size() << endl;
s2[19] = 'x';
cout << s2 << endl; //hello world x
cout << s2.size() << endl;
string s3("hello world");
//将s3中有效字符个数增加到20个,多出位置用'x'进行填充
s3.resize(20,'x');
cout << s3 << endl; //hello worldxxxxxxxxx
cout << s3.size() << endl;
string s4("hello world");
//将s4中有效字符个数缩小到5个
s4.resize(5);
cout << s4 << endl; //hello
cout << s4.size() << endl;</