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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM