【C++】一篇文章带你深入了解string

04-06 阅读 0评论

【C++】一篇文章带你深入了解string

目录

  • 一. 为什么学习string?
  • 二、 标准库中的string
    • 2.1 string介绍
    • 2.2 string的常用接口说明
      • 2.2.1 string对象的常见构造
        • 2.2.1.1 [string()](https://legacy.cplusplus.com/reference/string/string/string/) ---- 无参构造函数
        • 2.2.1.2 [string(const char* s)](https://legacy.cplusplus.com/reference/string/string/string/) ---- 有参构造函数
        • 2.2.1.3 [string(size_t n, char c)](https://legacy.cplusplus.com/reference/string/string/string/) ---- 有参构造函数
        • 2.2.1.4 [string(const string&s)](https://legacy.cplusplus.com/reference/string/string/string/) ---- 拷贝构造函数
        • 2.2.2 string对象的容量操作
          • 2.2.2.1 [size 函数](https://legacy.cplusplus.com/reference/string/string/size/)
          • 2.2.2.2 [length 函数](https://legacy.cplusplus.com/reference/string/string/length/)
          • 2.2.2.3 [capacity 函数](https://legacy.cplusplus.com/reference/string/string/capacity/)
          • 2.2.2.4[ empty 函数](https://legacy.cplusplus.com/reference/string/string/empty/)
          • 2.2.2.5 [clear 函数](https://legacy.cplusplus.com/reference/string/string/clear/)
          • 2.2.2.6 [reserve 函数](https://legacy.cplusplus.com/reference/string/string/reserve/)
          • 2.2.2.7 [resize 函数](https://legacy.cplusplus.com/reference/string/string/resize/)
          • 2.2.3 string对象的访问及遍历操作
            • 2.2.3.1 [operator[]](https://legacy.cplusplus.com/reference/string/string/operator%5B%5D/)
            • 2.2.3.2 迭代器 [begin ](https://legacy.cplusplus.com/reference/string/string/begin/)、[end](https://legacy.cplusplus.com/reference/string/string/end/)
            • 2.2.3.3 迭代器 [rbegin](https://legacy.cplusplus.com/reference/string/string/rbegin/) 、[rend](https://legacy.cplusplus.com/reference/string/string/rend/)
            • 2.2.3.4 范围for
            • 2.2.4. string对象的增删查改
              • 2.2.4.1 [push_back 函数](https://legacy.cplusplus.com/reference/string/string/push_back/)
              • 2.2.4.2 [operator+=](https://legacy.cplusplus.com/reference/string/string/operator+=/)
              • 2.2.4.3 [append 函数](https://legacy.cplusplus.com/reference/string/string/append/)
              • 2.2.4.4 [insert 函数](https://legacy.cplusplus.com/reference/string/string/insert/)
              • 2.2.4.5 [erase 函数](https://legacy.cplusplus.com/reference/string/string/erase/)
              • 2.2.4.6 [npos](https://legacy.cplusplus.com/reference/string/string/npos/)
              • 2.2.4.7 [c_str 函数](https://legacy.cplusplus.com/reference/string/string/c_str/)
              • 2.2.4.8 [find 函数](https://legacy.cplusplus.com/reference/string/string/find/)
              • 2.2.4.9 [rfind 函数](https://legacy.cplusplus.com/reference/string/string/rfind/)
              • 2.2.4.10 [substr 函数](https://legacy.cplusplus.com/reference/string/string/substr/)
              • 三、string拷贝问题
                • 3.1 经典的string问题
                • 3.2 浅拷贝
                • 3.3 深拷贝
                • 3.4 写时拷贝
                • 四、string的模拟实现
                  • 4.1 string默认成员函数的实现
                  • 4.2 string 中 c_str 、size 、capacity 和 empty 的实现
                  • 4.3 string 中 resize 和 reverse 的实现
                  • 4.4 string 中 push_back 、append 和 operator+= 的实现
                  • 4.5 string 中 operator[] 的实现
                  • 4.6 string 中 迭代器 的实现
                  • 4.7 string 中 operator> 的实现
                  • 4.8 string 中 比较函数 的实现
                  • 4.9 string 中 find 的实现
                  • 4.10 string 中 insert 和 erase 的实现
                  • 4.11 string 中 substr 的实现
                  • 4.12 string 实现汇总及函数测试
                  • 结尾

                    一. 为什么学习string?

                    C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。


                    二、 标准库中的string

                    2.1 string介绍

                    string的文档介绍

                    1. 字符串是表示字符序列的类。
                    2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。
                    3. string是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型(关于模板的更多信息,请参阅basic_string)。
                    4. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits和allocator作为basic_string的默认参数(根于更多的模板信息请参考basic_string)。
                    5. 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。

                    总结:

                    1. string是表示字符串的字符串类
                    2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
                    3. string在底层实际是:basic_string模板类的别名,typedef basic_stringstring;
                    4. 不能操作多字节或者变长字符的序列。

                    使用string时,必须包含#include头文件以及using namespace std;


                    2.2 string的常用接口说明

                    2.2.1 string对象的常见构造

                    2.2.1.1 string() ---- 无参构造函数
                    构造空的string对象,即空字符串
                    
                    #include 
                    #include 
                    using namespace std;
                    int main()
                    {
                    	string s;
                    	cout 
                    	string s("chineseprson");
                    	cout 
                    	string s(10,'z');
                    	cout 
                    	string s("chineseprson");
                    	string ss(s);
                    	cout 
                    	string s("chineseprson");
                    	cout 
                    	string s("chineseprson");
                    	cout 
                    	string s("chineseprson");
                    	cout 
                    	string s;
                    	string ss("chineseprson");
                    	cout 
                    	string s("chineseprson");
                    	cout 
                    	string s("chineseprson");
                    	s.reserve(30);
                    	cout 
                    	string s("chineseprson");
                    	cout 
                    	string s("chineseprson");
                    	for (int i = 0; i 

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,人围观)

还没有评论,来说两句吧...

目录[+]