# std::string ​ string是C++标准库中的`字符串类`,其对字符串的相关使用进行了封装。实现了`自动增长`和`越界保护`。并且对常用的操作符进行了重载,使其更易于使用。 参考: [cppreference](https://zh.cppreference.com/w/cpp/string/basic_string) [手册网](http://www.shouce.ren/api/c/index.htm) ## 一、构造函数 ```C++ string(); string( size_type length, char ch ); string( const char *str ); string( const char *str, size_type length ); string( string &str, size_type index, size_type length ); string( input_iterator start, input_iterator end ); ``` ## 二、操作符 > == > < >= <= != + += [] ​ string对上述操作符进行了重载。 ## 三、添加 ​ 添加文本可以直接使用重载的`+`号,但提供的`append`函数实现了更过功能: ```C++ basic_string &append( const basic_string &str ); basic_string &append( const char *str ); basic_string &append( const basic_string &str, size_type index, size_type len ); basic_string &append( const char *str, size_type num ); basic_string &append( size_type num, char ch ); basic_string &append( input_iterator start, input_iterator end ); ``` ## 四、赋值和拷贝 ### 1.赋值 ​ 赋值可以使用重载的`=`号,同样,`assign`函数提供了更多功能: ```C++ basic_string &assign( const basic_string &str ); basic_string &assign( const char *str ); basic_string &assign( const char *str, size_type num ); basic_string &assign( const basic_string &str, size_type index, size_type len ); basic_string &assign( size_type num, char ch ); ``` ### 2. 拷贝 ```C++ size_type copy( char *str, size_type num, size_type index ); ``` ​ `copy`函数拷贝自己的num个字符到str中(从index开始复制)。返回值为拷贝的字符数。 ## 五、插入 ```C++ iterator insert( iterator i, const char &ch ); basic_string &insert( size_type index, const basic_string &str ); basic_string &insert( size_type index, const char *str ); basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num ); basic_string &insert( size_type index, const char *str, size_type num ); basic_string &insert( size_type index, size_type num, char ch ); void insert( iterator i, size_type num, const char &ch ); void insert( iterator i, iterator start, iterator end ); ``` - 在迭代器i表示的位置前面插入一个字符ch, - 在字符串的位置index插入字符串str, - 在字符串的位置index插入字符串str的子串(从*index2*开始,长num个字符), - 在字符串的位置index插入字符串str的num个字符, - 在字符串的位置index插入num个字符ch的拷贝, - 在迭代器i表示的位置前面插入num个字符ch的拷贝, - 在迭代器i表示的位置前面插入一段字符,从start开始,以end结束. ## 六、切片 ```C++ basic_string substr( size_type index, size_type num = npos ); ``` ​ substr()返回本字符串的一个子串,从index开始,长num个字符。 ## 七、替代 ```C++ basic_string &replace( size_type index, size_type num, const basic_string &str ); basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,size_type num2 ); basic_string &replace( size_type index, size_type num, const char *str ); basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 ); basic_string &replace( size_type index, size_type num1, size_type num2, char ch ); basic_string &replace( iterator start, iterator end, const basic_string &str ); basic_string &replace( iterator start, iterator end, const char *str ); basic_string &replace( iterator start, iterator end, const char *str, size_type num ); basic_string &replace( iterator start, iterator end, size_type num, char ch ); ``` - 用str中的num个字符替换本字符串中的字符,从index开始 - 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符 - 用str中的num个字符(从index开始)替换本字符串中的字符 - 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符 - 用num2个ch字符替换本字符串中的字符,从index开始 - 用str中的字符替换本字符串中的字符,迭代器start和end指示范围 - 用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围, - 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围. ## 八、索引 1. 使用重载的`[]`符号,如果使用这个进行索引,则string不进行越界检查。 2. 使用`at`方法: ```C++ reference at( size_type index ); ``` ​ `at`方法提供了`越界检查`,当索引越界时,将会引发`out_of_range`异常。 ## 九、比较 ​ 比较可以使用重载的`<`、`>`、`==`、`!=`,但是`compare`函数提供了更多的功能: ```C++ int compare( const basic_string &str ); int compare( const char *str ); int compare( size_type index, size_type length, const basic_string &str ); int compare( size_type index, size_type length, const basic_string &str, size_type index2, size_type length2 ); int compare( size_type index, size_type length, const char *str, size_type length2 ); ``` 返回值如下: | 返回值 | 情况 | | ------ | --------- | | 小于零 | thisstr | 函数作用: - 比较自己和str, - 比较自己的子串和str,子串以index索引开始,长度为length - 比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己 - 比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length ## 十、查找 ### 1.find() ```C++ size_type find( const basic_string &str, size_type index ); size_type find( const char *str, size_type index ); size_type find( const char *str, size_type index, size_type length ); size_type find( char ch, size_type index ); ``` - 返回str在字符串中第一次出现的位置(从index开始查找)*。*如果没找到则返回**string::npos**, - 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回**string::npos**, - 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回**string::npos** ### 2.find_first_of() ```C++ size_type find_first_of( const basic_string &str, size_type index = 0 ); size_type find_first_of( const char *str, size_type index = 0 ); size_type find_first_of( const char *str, size_type index, size_type num ); size_type find_first_of( char ch, size_type index = 0 ); ``` - 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回**string::npos** - 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回**string::npos,** - 查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始。 ### 3.find_first_not_of() ```C++ size_type find_first_not_of( const basic_string &str, size_type index = 0 ); size_type find_first_not_of( const char *str, size_type index = 0 ); size_type find_first_not_of( const char *str, size_type index, size_type num ); size_type find_first_not_of( char ch, size_type index = 0 ); ``` - 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回**string::nops** - 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回**string::nops** - 在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回**string::nops** ### 4.find_last_of() ```C++ size_type find_last_of( const basic_string &str, size_type index = npos ); size_type find_last_of( const char *str, size_type index = npos ); size_type find_last_of( const char *str, size_type index, size_type num ); size_type find_last_of( char ch, size_type index = npos ); ``` - 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回**string::nops** - 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回**string::nops** - 在字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回**string::nops** ### 5.find_last_not_of ```C++ size_type find_last_not_of( const basic_string &str, size_type index = npos ); size_type find_last_not_of( const char *str, size_type index = npos); size_type find_last_not_of( const char *str, size_type index, size_type num ); size_type find_last_not_of( char ch, size_type index = npos ); ``` ## 十一、其他函数 | 函数 | 作用 | | :--------------------------------: | :---------------------------------: | | void **clear()** noexcept | 清除内容 | | void push_back(CharT ch) | 若导致`size()>max_size()`,抛出异常 | | void pop_back() noexcept | 从字符串移除末字符 | | void **swap( basic_string &str )** | 把str和本字符串交换 | 元素访问 | 函数 | 作用 | | --------------------------------------- | ------------------------------ | | const CharT& **front()** const | 返回首字符的引用 | | const CharT& **back**() const | 返回字符串中的末字符 | | const **char *data()** | 返回指向自己的第一个字符的指针 | | const **CharT* c_str()** const noexcept | 返回c风格的string,带有\0。 | 容量 | 函数 | 作用 | | ----------------------------------------- | ------------------------------------------------------------ | | bool **empty**() | 字符串为空则返回true,否则返回false | | size_type **size**() | 返回字符串中现在拥有的字符数 | | size_type **length**() | 返回字符串的长度,应该和size()返回的数字相同 | | size_type **max_size**() | 返回string能保存的最大字符数 | | void **reserve( size_type num )** | 设置本string的存储空间 | | size_type **capacity**() | 返回string在不重新申请空间的情况下还可以储存的字符数 | | void **resize( size_type num )** | 改变本字符串的大小到*num*, 新空间的内容不确定 | | void **resize( size_type num, char ch )** | 改变本字符串的大小到*num*,并使用ch进行填充 | | void **shrink_to_fit()** | 减少 [capacity()](https://zh.cppreference.com/w/cpp/string/basic_string/capacity) 到 [size()](https://zh.cppreference.com/w/cpp/string/basic_string/size) 的非强制请求。是否满足请求取依赖于实现 | 数值转换 | 函数 | 作用 | | ------------------------------------------------------------ | ------------------------------------------------------ | | std::**atoi**(const std::string&str,std::size_t* pos=0,int base=10) | str 指要处理的字符,base使数的进制,返回值为无符号整数 | | stol | 同上 | | stoll | 同上 | | stoul | 返回值为无符号整数 | | stoull | 同上 | | to_string | 将整数或浮点数转换为string | ## 十二、迭代器 ### 1. begin()和end() ```C++ iterator begin(); //返回一个迭代器,指向string的首字符 iterator end(); //返回一个迭代器,指向string的最后一个字符的下一个位置 ``` ### 2.erase() ```C++ iterator erase( iterator pos ); iterator erase( iterator start, iterator end ); basic_string &erase( size_type index = 0, size_type num = npos ); ``` - 删除pos指向的字符, 返回指向下一个字符的[迭代器](http://www.shouce.ren/api/c/iterators.html), - 删除从start到end的所有字符, 返回一个[迭代器](http://www.shouce.ren/api/c/iterators.html),指向被删除的最后一个字符的下一个位置 - 删除从index索引开始的num个字符, 返回***this**. ### 3.rbegin ```C++ const reverse_iterator rbegin() ``` ​ rbegin()返回一个逆向[迭代器](http://www.shouce.ren/api/c/iterators.html),指向字符串的最后一个字符 ### 4.rend ```C++ ` const reverse_iterator rend(); ` ``` rend()函数返回一个逆向[迭代器](http://www.shouce.ren/api/c/iterators.html),指向字符串的开头(第一个字符的前一个位置)。 ### 5.rfind ```c++ size_type rfind( const basic_string &str, size_type index ); size_type rfind( const char *str, size_type index ); size_type rfind( const char *str, size_type index, size_type num ); size_type rfind( char ch, size_type index ); ``` - 返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回**string::npos** - 返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回**string::npos** - 返回最后一个与ch匹配的字符,从index开始查找。如果没找到就返回**string::npos** # 迭代器 尽管使用 string[1] 也可以拿到数据,但是在使用 erease 的时候必须使用迭代器 string.begin() + 1。要不然会出现一起奇怪的行为