文章目录
- 为什么要学习string?
- 库中的string
- 关于编码
- ASCII编码
- Unicode编码
- 迭代器Iterators
- string常用构造接口
- 接口声明与功能说明
- 接口演示
- string类对象的容量操作
- 接口声明与功能说明
- 接口演示
- reverse与resize在不同平台下的扩容与缩容机制
- string类对象的访问及遍历操作
- 接口声明与功能说明
- 接口演示
- at与operator[] 的区别
- back与front
- string类对象的修改操作
- operator+=
- append
- push_back与pop_back
- insert
- erase
- replace
- swap
- c_str
- find与rfind
- substr
- string类非成员函数
- operator+
- relational operators
- getline ()
- vs和g++下string结构的说明
- vs下string的结构
- g++下string的结构
为什么要学习string?
提高效率!提高效率!提高效率!
发展是滚滚向前的,我们是站在巨人肩膀上的一代,C++中有前辈为我们准备好的STL库,对于想要实现某种结构不再需要我们去手搓某一结构供我们使用,对于手搓某一已经被实现的结构再来使用这种行为是对生产力的浪费!
库中的string
库中的string类是由basic_string类模板实例化而来,原因是因为string是专门用于表示字符序列的对象,而为了能够适应多种语言(汉语,英语,韩语…),basic_string类模也就实例化了不同编码方式的类。
string编码规则是使用单字节编码,通常采用 ASCII 或 UTF-8 编码
u16string编码规则是UTF-16 编码
u32string编码规则是UTF-32 编码
wstring编码规则是宽字符编码,可以是 UTF-16 或 UTF-32,具体取决于平台
关于编码
大家思考在计算机中会存储像"哦豁,记得关注博主"这样的字符串内容吗?
当然不会,在计算机底层存储的是二进制序列,像00001111这样的序列(实际上计算机存储的是一种可以用来表示1或0的状态)。
ASCII编码
现在我们就来了解一下ASCII编码。
以下是ASCII编码表:
在ASCII编码规则中,一个英文字母用一个字节表示,一个汉字用多个字节表示(对于常见汉字一个汉字用两个字节表示)。
为什么出现这样的规则?
对于英文来讲,无非就是26个英文字母大小写,一个字节就够用了,对于博大精深的汉语来讲,汉字数不胜数,一个字节最多才能表示256个汉字于是就用多个字节表示一个汉字,而在世界上有那么多种语言,那他们怎么表示呢?为了解决这个问题,也是就出现了多种编码方式,这也就是为什么basic_string要实例化多种类。
我们可以通过内存存储与sizeof,验证ASCII编码规则。
#include using namespace std; int main() { char Estr[] = "apple"; char Cstr[] = "哦豁,记得关注博主哦"; cout string s1; string s2("hello world!"); string s3(s2); string s4(s2,1,7); string s5("hello world",8); string s6(10,'a'); string s7(s2, 2, 3); string s8{ "hello world" }; string s9(string("hello world")); cout string s1("hello world"); cout string s1("abcde"); try { cout } } string s1("abcde"); cout string s1("abcde"); cout string link("xxx"); string s1; s1 += link; cout std::string line; std::getline(std::cin, line); // 读取一行,直到遇到换行符 std::cout // storage for small buffer or pointer to larger one value_type _Buf[_BUF_SIZE]; pointer _Ptr; char _Alias[_BUF_SIZE]; // to permit aliasing } _Bx;
还没有评论,来说两句吧...