C++ string类型详解

来源:互联网 发布:淘宝口碑三文鱼 编辑:程序博客网 时间:2024/06/14 04:41

C++ string类型详解



string是非常强大的类型,很好的封装了字符串的操作,有些时候我们可以把string当做字符的容器,string也

支持大多数容器操作,下面就列出string类型所支持的所有操作,本文并不是为了讲解string的用法和应用,

而是希望作为string类型的参考文档,每个函数皆在注释后有详细说明,需要用时查阅即可。

1.构造函数

[cpp] view plain copy
  1. string();//空串  
  2.   
  3. string(size_type length,char ch);//以length为长度的ch的拷贝(即length个ch)  
  4.   
  5. string(const char *str);//以str为初值 (长度任意)  
  6.   
  7. string(const char *str,size_type length);//同上,长度不限,但注意不要越界,以免发生不可预知问题  
  8.   
  9. string(string &str, size_type index, size_type length);  
  10. //以index为索引开始的子串,长度为length, 或者小于length  
  11.   
  12. string(input_iterator begin, input_iterator end);//以从start到end的元素为初值  
2.支持的操作符
    ==
    >
    <
    >=
    <=
    !=
    +
    +=
    [  ]

3.追加文本(append)

[cpp] view plain copy
  1. basic_string &append(const basic_string &str);//在字符串的末尾添加str  
  2.   
  3. basic_string &append(const char *str);//在字符串末尾添加str所指向的c风格字符串  
  4.   
  5. basic_string &append(const basic_string &str,size_type index,size_type len);  
  6. //在字符串的末尾添加str的子串,子串以index索引开始,长度为len  
  7.   
  8. basic_string &append(const char *str,size_type num);//在字符串的末尾添加str中的num个字符  
  9.   
  10. basic_string &append(size_type num,char ch);//在字符串的末尾添加num个字符ch  
  11.   
  12. basic_string &append(input_iterator start,input_iterator end);  
  13. //在字符串的末尾添加以迭代器start和end表示的字符序列  
  14.   
  15. push_back('k');//把一个字符连接到当前字符串的结尾  

4.赋值(assign)

[cpp] view plain copy
  1. basic_string &assign(const basic_string &str);//用str为字符串赋值  
  2.   
  3. basic_string &assign(const char *str);//用str c风格为字符串赋值  
  4.   
  5. basic_string &assign(const char *str,size_type num);//用str的开始num个字符为字符串赋值  
  6.   
  7. basic_string &assign(const basic_string &str,size_type index,size_type len);  
  8. //用str的子串为字符串赋值,子串以index索引开始,长度为len  
  9.   
  10. basic_string &assign(size_type num,char ch);//用num个字符ch为字符串赋值  
  11.   
  12. string &assign(const_iterator begin,const_itertor end);  
  13. //把first和last迭代器之间的部分赋给字符串  

5.比较(compare)

[cpp] view plain copy
  1. int compare(const basic_string &str);//比较自己和str  
  2.   
  3. int compare(size_type index,size_type length,const basic_string &str);  
  4. //比较自己的子串和str,子串以index索引开始,长度为length  
  5.   
  6. int compare(size_type index,size_type length,const basic_string &str,size_type   
  7.             index2,size_type length2);  
  8. //比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己  
  9.   
  10. int compare(const char *str);//比较自己和str  
  11.   
  12. int compare(int pos, int n,const char *s)  
  13. //比较自己的子串,从pos开始,n个字符,和s进行比较  
  14.   
  15. int compare(size_type index,size_type length,const char *str,size_type length2);  
  16. //比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串  
  17. //以index开始,长度为length  

     返回值        情况

      小于零        this < str
      零                this == str
      大于零        this > str

6.删除(erase)

[cpp] view plain copy
  1. iterator erase(iterator first, iterator last);  
  2. //删除[first,last)之间的所有字符,返回删除后迭代器的位置  
  3.   
  4. iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置  
  5.   
  6. string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串  

7.插入(insert)

[cpp] view plain copy
  1. iterator insert(iterator i,const char &ch);//在迭代器i表示的位置前面插入一个字符ch  
  2.   
  3. basic_string &insert(size_type index,const basic_string &str);//在字符串的位置index插入字符串str  
  4.   
  5. basic_string &insert(size_type index,const char *str);//在字符串的位置index插入字符串str  
  6.   
  7. basic_string &insert(size_type index1,const basic_string &str,size_type index2,size_type num);  
  8. //在字符串的位置index插入字符串str的子串(从index2开始,长num个字符)  
  9.   
  10. basic_string &insert(size_type index,const char *str,size_type num);  
  11. //在字符串的位置index插入字符串str的num个字符  
  12.   
  13. basic_string &insert(size_type index,size_type num,char ch );  
  14. //在字符串的位置index插入num个字符ch的拷贝  
  15.   
  16. void insert(iterator i,size_type num,const char &ch);  
  17. //在迭代器i表示的位置前面插入num个字符ch的拷贝  
  18.   
  19. void insert(iterator i,iterator begin,iterator end );  
  20. //在迭代器i表示的位置前面插入一段字符,从start开始,以end结束  

8.替换(replace)

[cpp] view plain copy
  1. basic_string &replace(size_type index,size_type num,const basic_string &str);  
  2. //用str中的num个字符替换本字符串中的字符,从index开始  
  3.   
  4. replace(size_type index1,size_type num1,const basic_string &str,size_type index2,size_type num2);  
  5. //用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符  
  6.   
  7. basic_string &replace(size_type index,size_type num,const char *str);  
  8. //用str中的num个字符(从index开始)替换本字符串中的字符  
  9.   
  10. basic_string &replace(size_type index,size_type num1,const char *str,size_type num2);  
  11. //用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符  
  12.   
  13. basic_string &replace(size_type index,size_type num1,size_type num2,char ch);  
  14. //用num2个ch字符替换本字符串中的字符,从index开始,num1个字符  
  15.   
  16. basic_string &replace(iterator start,iterator end,const basic_string &str);  
  17. //用str中的字符替换本字符串中的字符,迭代器start和end指示范围  
  18.   
  19. basic_string &replace(iterator start,iterator end,const char *str);  
  20. //用str替换本字符串中的内容,迭代器start和end指示范围  
  21.   
  22. basic_string &replace(iterator start,iterator end,const char *str,size_type num );  
  23. //用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围  
  24.   
  25. basic_string &replace(iterator start,iterator end,size_type num,char ch );  
  26. //用num个ch字符替换本字符串中的内容,迭代器start和end指示范围  

9.查找

[cpp] view plain copy
  1. 函数 find:  
  2.   
  3. size_type find( const basic_string &str, size_type index );  
  4. //返回str在字符串中第一次出现的位置(从index开始查找)  
  5.   
  6. size_type find( const char *str, size_type index );  
  7. //返回str在字符串中第一次出现的位置(从index开始查找)  
  8.   
  9. size_type find( const char *str, size_type index, size_type length );  
  10. //返回str在字符串中第一次出现的位置(从index开始查找,长度为length)  
  11.   
  12. size_type find( char ch, size_type index );  
  13. //返回字符ch在字符串中第一次出现的位置(从index开始查找)  
  14.   
  15.   
  16. 函数 find_first_of:查找在字符串中第一个与str中的某个字符匹配的字符  
  17.   
  18.     size_type find_first_of( const basic_string &str, size_type index = 0);  
  19.   
  20.     size_type find_first_of( const char *str, size_type index = 0 );  
  21.   
  22.     size_type find_first_of( const char *str, size_type index, size_type num );  
  23.   
  24.     size_type find_first_of( char ch, size_type index = 0 );  
  25.   
  26. 函数 find_first_not_of:在字符串中查找第一个与str中的字符都不匹配的字符  
  27.   
  28.     size_type find_first_not_of( const basic_string &str, size_type index = 0 );  
  29.   
  30.     size_type find_first_not_of( const char *str, size_type index = 0 );  
  31.   
  32.     size_type find_first_not_of( const char *str, size_type index, size_type num );  
  33.   
  34.     size_type find_first_not_of( char ch, size_type index = 0 );  
  35.   
  36. 函数 find_last_of:在字符串中查找最后一个与str中的某个字符匹配的字符  
  37.   
  38.   size_type find_last_of( const basic_string &str, size_type index = npos );  
  39.   
  40.   size_type find_last_of( const char *str, size_type index = npos );  
  41.   
  42.   size_type find_last_of( const char *str, size_type index, size_type num );  
  43.   
  44.   size_type find_last_of( char ch, size_type index = npos );  
  45.   
  46. 函数 find_last_not_of:在字符串中查找最后一个与str中的字符都不匹配的字符  
  47.   
  48.     size_type find_last_not_of( const basic_string &str, size_type index = npos );  
  49.   
  50.     size_type find_last_not_of( const char *str, size_type index = npos);  
  51.   
  52.     size_type find_last_not_of( const char *str, size_type index, size_type num );  
  53.   
  54.     size_type find_last_not_of( char ch, size_type index = npos );  
  55.   
  56. rfind函数  
  57.   
  58.   size_type rfind( const basic_string &str, size_type index );  
  59.   //返回最后一个与str中的某个字符匹配的字符,从index开始查找  
  60.   
  61.   size_type rfind( const char *str, size_type index );  
  62.   //返回最后一个与str中的某个字符匹配的字符,从index开始查找  
  63.   
  64.   size_type rfind( const char *str, size_type index, size_type num );  
  65.   //返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符  
  66.   
  67.   size_type rfind( char ch, size_type index );  
  68.   //返回最后一个与ch匹配的字符,从index开始查找  


10.其他函数

[cpp] view plain copy
  1. at函数  
  2.      reference at( size_type index );  
  3.      //at()函数返回一个引用,指向在index位置的字符. 如果index  
  4.      //不在字符串范围内, at() 将报告"out of range"错误,并抛出out_of_range异常  
  5.   
  6. begin函数  
  7.     iterator begin();//begin()函数返回一个迭代器,指向字符串的第一个元素  
  8.   
  9. end函数  
  10.     iterator end();//返回一个迭代器,指向字符串的末尾(最后一个字符的下一个位置)  
  11.   
  12. c_str函数  
  13.     const char *c_str();//返回一个指向正规C字符串的指针, 内容与本字符串相同  
  14.   
  15. capacity函数  
  16.     size_type capacity();//返回在重新申请更多的空间前字符串可以  
  17.     //容纳的字符数. 这个数字至少与 size()一样大  
  18.   
  19. copy函数  
  20.     size_type copy( char *str, size_type num, size_type index );  
  21.     //拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数  
  22.   
  23. data函数  
  24.     const char *data();//返回指向自己的第一个字符的指针  
  25.   
  26. empty函数  
  27.     bool empty();//如果字符串为空则empty()返回真(true),否则返回假(false)  
  28.   
  29. get_allocator函数  
  30.     allocator_type get_allocator();//返回本字符串的配置器  
  31.   
  32. length函数  
  33.     size_type length();//返回字符串的长度. 这个数字应该和size()返回的数字相同  
  34.   
  35. max_size  
  36.     size_type max_size();//返回字符串能保存的最大字符数  
  37.   
  38. rbegin函数  
  39.     rbegin();//返回一个逆向迭代器,指向最后一个字符  
  40.   
  41. rend函数  
  42.     rend();//返回一个逆向迭代器,指向第一个元素的前一个位置  
  43.   
  44. reserve函数  
  45.     reserve( size_type num );//保留一定容量以容纳字符串(设置capacity值)  
  46.   
  47. resize函数  
  48.   void resize( size_type num );//改变本字符串的大小到num, 新空间的内容不确定  
  49.   
  50.   void resize( size_type num, char ch );//也可以指定用ch填充  
  51.   
  52. size函数  
  53.     size();//返回字符串中字符的数量  
  54.   
  55. substr函数  
  56.      basic_string substr( size_type index, size_type num = npos );  
  57.      //返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,  
  58.      //将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串  
  59.   
  60. swap函数  
  61.     void swap( basic_string &str );//把str和本字符串交换  

11.示例

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5. int main(){  
  6.     //1.string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作  
  7.     string str1;  
  8.     cin >> str1;//当用cin>>进行字符串的输入的时候,遇到空格的地方就停止字符串的读取输入  
  9.     cout << str1 << endl;  
  10.     cin.get();//这个的作用就是读取cin>>输入的结束符,不用对getline的输入产生影响!  
  11.     getline(cin, str1);//字符串的行输入  
  12.     cout << str1 << endl;  
  13.   
  14.     //2.string类的构造函数  
  15.     string str2 = "aaaaa";//最简单的字符串初始化  
  16.     cout << str2 << endl;  
  17.   
  18.     char *s = "bbbbb";  
  19.     string str3(s);//用c字符串s初始化  
  20.     cout << str3 << endl;  
  21.   
  22.     char ch = 'c';  
  23.     string str4(5, ch);//用n个字符ch初始化  
  24.     cout << str4 << endl;  
  25.   
  26.     //3.string类的字符操作  
  27.     string str5 = "abcde";  
  28.     ch = str5[3];//operator[]返回当前字符串中第n个字符的位置  
  29.     cout << ch << endl;  
  30.   
  31.     string str6 = "abcde";  
  32.     ch = str6.at(4);//at()返回当前字符串中第n个字符的位置,并且提供范围检查,当越界时会抛出异常!  
  33.     cout << ch << endl;  
  34.   
  35.     //4.string的特性描述  
  36.     string str7 = "abcdefgh";  
  37.     int size;  
  38.     size = str7.capacity();//返回当前容量  
  39.     cout << size << endl;  
  40.     size = str7.max_size();//返回string对象中可存放的最大字符串的长度  
  41.     cout << size << endl;  
  42.     size = str7.size();//返回当前字符串的大小  
  43.     cout << size << endl;  
  44.     size = str7.length();//返回当前字符串的长度  
  45.     cout << size << endl;  
  46.     bool flag;  
  47.     flag = str7.empty();//判断当前字符串是否为空  
  48.     cout << flag << endl;  
  49.     int len = 10;  
  50.     str7.resize(len, ch);//把字符串当前大小置为len,并用字符ch填充不足的部分  
  51.     cout << str7 << endl;  
  52.   
  53.     //5.string的赋值  
  54.     string str8;  
  55.     str8 = str7;//把字符串str7赋给当前字符串  
  56.     cout << str8 << endl;  
  57.     str8.assign(str7);//把字符串str7赋给当前字符串  
  58.     cout << str8 << endl;  
  59.     str8.assign(s);//用c类型字符串s赋值  
  60.     cout << str8 << endl;  
  61.     str8.assign(s, 2);//用c类型字符串s开始的n个字符赋值  
  62.     cout << str8 << endl;  
  63.     str8.assign(len, ch);//用len个字符ch赋值给当前字符串  
  64.     cout << str8 << endl;  
  65.     str8.assign(str7, 0, 3);//把字符串str7中从0开始的3个字符赋给当前字符串  
  66.     cout << str8 << endl;  
  67.     string str9 = "0123456789";  
  68.     str8.assign(str9.begin(), str9.end());//把迭代器之间的字符赋给字符串  
  69.     cout << str8 << endl;  
  70.   
  71.     //6.string的连接  
  72.     string str10;  
  73.     str10 += str9;//把字符串str9连接到当前字符串的结尾  
  74.     cout << str10 << endl;  
  75.     str10.append(s);//把c类型字符串s连接到当前字符串的结尾  
  76.     cout << str10 << endl;  
  77.     str10.append(s, 2);//把c类型字符串s的前2个字符连接到当前字符串的结尾  
  78.     cout << str10 << endl;  
  79.     str10.append(str9.begin(), str9.end());//把迭代器之间的一段字符连接到当前字符串的结尾  
  80.     cout << str10 << endl;  
  81.     str10.push_back('k');//把一个字符连接到当前字符串的结尾  
  82.     cout << str10 << endl;  
  83.   
  84.     //7.string的比较  
  85.     flag = (str9 == str10);//判断两个字符串是否相等  
  86.     cout << flag << endl;  
  87.     flag = (str9 != str10);//判断两个字符串是否不相等  
  88.     cout << flag << endl;  
  89.     flag = (str9 > str10);//判断两个字符串是否大于关系  
  90.     cout << flag << endl;  
  91.     flag = (str9 < str10);//判断两个字符串是否为小于关系  
  92.     cout << flag << endl;  
  93.     flag = (str9 >= str10);//判断两个字符串是否为大于等于关系  
  94.     cout << flag << endl;  
  95.     flag = (str9 <= str10);//判断两个字符串否为小于等于关系  
  96.     cout << flag << endl;  
  97.   
  98.     //以下的3个函数同样适用于c类型的字符串,在compare函数中>时返回1,<时返回-1,=时返回0  
  99.     flag = str10.compare(str9);//比较两个字符串的大小,通过ASCII的相减得出!  
  100.     cout << flag << endl;  
  101.     flag = str10.compare(6, 12, str9);//比较str10字符串从6开始的12个字符组成的字符串与str9的大小  
  102.     cout << flag << endl;  
  103.     flag = str10.compare(6, 12, str9, 3, 5);//比较str10字符串从6开始的12个字符组成的字符串与str9字符串从3开始的5个字符组成的字符串的大小  
  104.     cout << flag << endl;  
  105.   
  106.     //8.string的字串  
  107.     string str11;  
  108.     str11 = str10.substr(10, 15);//返回从下标10开始的15个字符组成的字符串  
  109.     cout << str11 << endl;  
  110.   
  111.     //9.string的交换  
  112.     str11.swap(str10);//交换str11与str10的值  
  113.     cout << str11 << endl;  
  114.   
  115.     //10.string的查找,查找成功时返回所在位置,失败时返回string::npos的值,即是-1  
  116.     string str12 = "abcdefghijklmnopqrstuvwxyz";  
  117.     int pos;  
  118.     pos = str12.find('i', 0);//从位置0开始查找字符i在当前字符串的位置  
  119.     cout << pos << endl;  
  120.     pos = str12.find("ghijk", 0);//从位置0开始查找字符串“ghijk”在当前字符串的位置  
  121.     cout << pos << endl;  
  122.     pos = str12.find("opqrstuvw", 0, 4);//从位置0开始查找字符串“opqrstuvw”前4个字符组成的字符串在当前字符串中的位置  
  123.     cout << pos << endl;  
  124.     pos = str12.rfind('s', string::npos);//从字符串str12反向开始查找字符s在字符串中的位置  
  125.     cout << pos << endl;  
  126.     pos = str12.rfind("klmn", string::npos);//从字符串str12反向开始查找字符串“klmn”在字符串中的位置  
  127.     cout << pos << endl;  
  128.     pos = str12.rfind("opqrstuvw", string::npos, 3);//从string::pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置  
  129.     cout << pos << endl;  
  130.   
  131.     string str13 = "aaaabbbbccccdddeeefffggghhhiiijjjkkllmmmandjfaklsdfpopdtwptioczx";  
  132.     pos = str13.find_first_of('d', 0);//从位置0开始查找字符d在当前字符串第一次出现的位置  
  133.     cout << pos << endl;  
  134.     pos = str13.find_first_of("eefff", 0);//从位置0开始查找字符串“eeefff“在当前字符串中第一次出现的位置  
  135.     cout << pos << endl;  
  136.     pos = str13.find_first_of("efff", 0, 3);//从位置0开始查找当前串中第一个在字符串”efff“的前3个字符组成的数组里的字符的位置  
  137.     cout << pos << endl;  
  138.     pos = str13.find_first_not_of('b', 0);//从当前串中查找第一个不在串s中的字符出现的位置  
  139.     cout << pos << endl;  
  140.     pos = str13.find_first_not_of("abcdefghij", 0);//从当前串中查找第一个不在串s中的字符出现的位置  
  141.     cout << pos << endl;  
  142.     pos = str13.find_first_not_of("abcdefghij", 0, 3);//从当前串中查找第一个不在由字符串”abcdefghij”的前3个字符所组成的字符串中的字符出现的位置  
  143.     cout << pos << endl;  
  144.     //下面的last的格式和first的一致,只是它从后面检索!  
  145.     pos = str13.find_last_of('b', string::npos);  
  146.     cout << pos << endl;  
  147.     pos = str13.find_last_of("abcdef", string::npos);  
  148.     cout << pos << endl;  
  149.     pos = str13.find_last_of("abcdef", string::npos, 2);  
  150.     cout << pos << endl;  
  151.     pos = str13.find_last_not_of('a', string::npos);  
  152.     cout << pos << endl;  
  153.     pos = str13.find_last_not_of("abcdef", string::npos);  
  154.     cout << pos << endl;  
  155.     pos = str13.find_last_not_of("abcdef", string::npos, 3);  
  156.     cout << pos << endl;  
  157.   
  158.     //11.string的替换  
  159.     string str14 = "abcdefghijklmn";  
  160.     str14.replace(0, 3, "qqqq");//删除从0开始的3个字符,然后在0处插入字符串“qqqq”  
  161.     cout << str14 << endl;  
  162.     str14.replace(0, 3, "vvvv", 2);//删除从0开始的3个字符,然后在0处插入字符串“vvvv”的前2个字符  
  163.     cout << str14 << endl;  
  164.     str14.replace(0, 3, "opqrstuvw", 2, 4);//删除从0开始的3个字符,然后在0处插入字符串“opqrstuvw”从位置2开始的4个字符  
  165.     cout << str14 << endl;  
  166.     str14.replace(0, 3, 8, 'c');//删除从0开始的3个字符,然后在0处插入8个字符 c  
  167.     cout << str14 << endl;  
  168.     //上面的位置可以换为迭代器的位置,操作是一样的,在这里就不再重复了!  
  169.   
  170.     //12.string的插入,下面的位置处亦可以用迭代器的指针表示,操作是一样的  
  171.     string str15 = "abcdefg";  
  172.     str15.insert(0, "mnop");//在字符串的0位置开始处,插入字符串“mnop”  
  173.     cout << str15 << endl;  
  174.     str15.insert(0, 2, 'm');//在字符串的0位置开始处,插入2个字符m  
  175.     cout << str15 << endl;  
  176.     str15.insert(0, "uvwxy", 3);//在字符串的0位置开始处,插入字符串“uvwxy”中的前3个字符  
  177.     cout << str15 << endl;  
  178.     str15.insert(0, "uvwxy", 1, 2);//在字符串的0位置开始处,插入从字符串“uvwxy”的1位置开始的2个字符  
  179.     cout << str15 << endl;  
  180.   
  181.     //13.string的删除  
  182.     string str16 = "gfedcba";  
  183.     string::iterator it;  
  184.     it = str16.begin();  
  185.     it++;  
  186.     str16.erase(it);//删除it指向的字符,返回删除后迭代器的位置  
  187.     cout << str16 << endl;  
  188.     str16.erase(it, it+3);//删除it和it+3之间的所有字符,返回删除后迭代器的位置  
  189.     cout << str16 << endl;  
  190.     str16.erase(2);//删除从字符串位置3以后的所有字符,返回位置3前面的字符  
  191.     cout << str16 << endl;  
  192.   
  193.     //14.字符串的流处理  
  194.     string str17("hello,this is a test");  
  195.     istringstream is(str17);  
  196.     string s1,s2,s3,s4;  
  197.     is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"  
  198.     ostringstream os;  
  199.     os<<s1<<s2<<s3<<s4;  
  200.     cout<<os.str() << endl;  
  201.   
  202.     //system("pause");  
  203. }  


参考博客:http://blog.csdn.net/ro_wsy/article/details/7701218

1 0