C++ string 用法总结

  string查找替换、分割字符串、比较、截取、类型转换、排序等功能都提供了强大的处理函数,可以代替字符数组来使用。

熟练掌握好string的各种使用方法,能极大的提高编程效率哦 ^_^。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<string>
  6 #include<cmath>
  7 #include<algorithm>
  8 #include<vector>
  9 #include<queue>
 10 #include<fstream>
 11 using namespace std;
 12 int main(){
 13     
 14     //string 的初始化
 15     string str0,str00;
 16     cin>>str0; cout<<str0<<endl;//cin读入忽略空字符,遇见空格自动停止 
 17     getline(cin,str00); cout<<str00<<endl;//不忽略空格,但舍弃换行符 
 18     string str1="hello word";
 19     string str2=("my heart go on");
 20     string str3 (str1,5);//str3表示str1从第五个开始的之后的所有字符(不包含第 5个字符) 
 21     string str4 (str1,5,3);//str3表示str1从第五个开始的之后的3个字符(不包含第 5个字符) 
 22     char s[]={"bits/stdc++.h"};
 23     string str5=s;
 24     string str6 (s);
 25     string str7 (s,4);//==bits 和str3不一样  
 26     string str8 (10,'a');
 27     cout<<str1<<endl<<str2<<endl<<str3<<endl<<str4<<endl;
 28     cout<<s<<endl<<str5<<endl<<str6<<endl<<str7<<endl<<str8<<endl;
 29     
 30     //赋值,拼接字符串
 31     string str9="C18H20FN3O4";
 32     string str10="你好";
 33     string str11=str9+str10;//直接做加法拼接 
 34     cout<<str11<<endl; 
 35     cout<<str10+str9<<endl;
 36     
 37     str9.push_back('.');//加入单字符,不能加入字符串 
 38     cout<<str9<<endl;
 39     
 40     str9.append("you can see C18H20FN3O4.");//加入字符串 
 41     cout<<str9<<endl; 
 42     
 43     str9.assign("dreams come true");//重新赋值 
 44     cout<<str9<<endl;
 45     
 46     str9.insert(14,"!!!!!");//在指定位置插入字符串,是插入,不是覆盖 
 47     cout<<str9<<endl;
 48     
 49     //string 的访问
 50     string str12="C++isdifficult";
 51     cout<<str12[2]<<" "<<str12[6]<<" "<<str12.at(0)<<endl;//下标从0开始 
 52     
 53     //可以使用 STL 的接口 
 54     string str13;
 55     str13="GFBACED";
 56     string::iterator itstr=str13.begin();
 57     for( ;itstr!=str13.end();itstr++){//遍历 
 58         cout<<*itstr;
 59     }
 60     cout<<endl;
 61     sort(str13.begin(),str13.end());//可以快排 
 62     cout<<str13<<endl;
 63     
 64      /*
 65      比较操作 ==  !=  >  >=  <  <=  compare 等string的比较操作,按字符在字
 66          典中的顺序进行逐一比较。在字典前面的字符小于后面的字符。 
 67      */
 68      string str14="ABCDEF";
 69      string str15="ABCD";
 70      string str16="BA";
 71      if(str14<str15) cout<<"str14<str15";
 72      else cout<<"str15<str14"<<endl;
 73      if(str14<str16) cout<<"str14<str16";
 74      else cout<<"str16<str14"<<endl;
 75      
 76      //string 可以直接排序 
 77      string str17[10];
 78      for(int i=1;i<=5;i++) cin>>str17[i];
 79      sort(str17+1,str17+5+1);
 80      for(int i=1;i<=5;i++) cout<<str17[i]<<endl;
 81      
 82      
 83      //字符查找 
 84      string str18="i am a student,and i am 16 years old";
 85      string::size_type pos;
 86      pos=str18.find("am");
 87      if(pos!=str18.npos)
 88          cout<<"第一次出现的下标是:"<<pos<<endl;//下标从 0开始 
 89      pos=str18.find("am",10);
 90      cout<<"在下标10之后第一次出现的下标是"<<pos<<endl; 
 91      
 92      string str19="i";
 93      pos=str18.rfind(str19);//反向查找 
 94      cout<<"反向中第一次出现的位置"<<pos<<endl;
 95      
 96      
 97      //字符交换
 98      string str19="www.baidu.com";
 99      string str20="www.google.com";
100      str19.swap(str20);//交换 
101      cout<<str19<<" "<<str20<<endl;
102      str19.erase();//删除整个字符串 
103      str20.clear();//清空字符容器中所有内容 
104      cout<<str19<<" "<<str20<<endl;
105      
106      return 0;
107 }
原文地址:https://www.cnblogs.com/CXCXCXC/p/4861707.html