C++STL之string

在學習c++STL中的string,在這裏做個筆記,以供自己以後翻閱和初學者參考。

1:string對象的定義和初始化以及讀寫

string s1;      默認構造函數,s1爲空串

string s2(s1);   將s2初始化爲s1的一個副本

string s3("valuee");   將s3初始化一個字符串面值副本

string s4(n,'c');   將s4 初始化爲字符'c'的n個副本

cin>>s5;  讀取有效字符到遇到空格

getline(cin,s6);  讀取字符到遇到換行,空格可讀入,知道‘\n’結束(練習在下一個代碼中),

getline(cin,s7,'a'); 一個直到‘a’結束,其中任何字符包括'\n'都能夠讀入,可以試試題:UVa10361

下面看一個鞏固練習:

  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     string s1;  
  7.     s1="i love you";  
  8.     string s2(s1);  //把s2初始化爲s1的一個副本,注意寫法,不能前面先定義s2的類型後面直接寫,也不能定義兩次s2  
  9.     string s3("value");  //將s3初始化一個字符串面值副本  
  10.     string s4(10,'s');   //將s4初始化爲字符‘s'的10個副本  
  11.     /*注意字符串面值與標準庫string不是同一個類型*/  
  12.     cout<<s2<<" "<<s3<<" "<<s4<<endl;  
  13.     string s5;  
  14.     while(cin>>s5)  //這裏可以輸入“  hello world  ”測試,發現只讀取有效字符到遇到空格結束  
  15.     {  
  16.         cout<<s5<<endl;  
  17.     }  
  18.     return 0;  
  19. }  

2:string對象操作

s.empty()  判斷是否爲空,bool型

s.size() 或 s.length() 返回字符的個數

s[n]  返回位置爲n的字符,從0開始計數

s1+s2 連接,看下面例子:

    可用此方法給字符串後面添加字符如:s=s+'a'; 

    a:  string s2=s1+", ";  //對,把一個string對象和一個字符面值連接起來是允許的

    b:  string s4="hello "+", ";   //錯,不能將兩個字符串面值相加

    c:  string s5=s1+", "+"world";   //對,前面兩個相加相當於一個string對象;

    d:  string s6="hello" + ", " +  s2;  //錯

(注:字符串尾部追加還可用s.append("abc")函數添加)

s1=s2  替換

s1==s2  相等,返回true或false

!=,<,<=,>,>=  字符串比較,兩個字符串短的與長的前面匹配,短的小於長的

鞏固練習:

  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     string str1;  
  7.     string str2("the size of ");  
  8.     string str3=" hello world  ";//空格不會忽略  
  9.     str3+=str2;  
  10.     str3.append("haha secessful");  
  11.     cout<<str3<<endl;  
  12.     cout<<"the size of is "<<str2.size()<<endl;  
  13.     /*注意這裏取長度的str2.size(),和str2.length(),但是注意str2.size()返回的值並不是int類型, 
  14.     事實表明size_type存儲的string長度是int所能存儲的兩倍*/  
  15.     getline(cin,str1);  //read line at time until end-of-file,注意寫法。  
  16.     while(!str1.empty())  //返回一個bool值,空的話返回true,否則返回false。  
  17.     {  
  18.         for(string::size_type i=0;i!=str1.size();++i)  //注意size_type類型  
  19.         {  
  20.             cout<<str1[i];  
  21.         }  
  22.         cout<<endl;break;  
  23.     }  
  24.     return 0;  
  25. }  



3:string對象中字符的處理(頭文件cctype)

    isalnum(c)  如果c是字母或數字,返回 true

    isalpha(c)  如果c是字母,返回true

    iscntrl(c)  c是控制符,返回true

    isdigit(c)  如果c是數組,返回true

    isgraph(c)  如果c不是空格,則可打印,,則爲true

    islower(c)  如果c是小寫字母,則爲true

    isupper(c)  如果c是大寫字符,則爲true

    isprint(c)  如果c是可打印的字符,則爲true

    ispunct(c)  如果c是標點符號,則爲true

    isspace(c) 如果c是空白字符,則爲true

    isxdigit(c) 如果c是十六進制數,則爲true

    tolower(c) 如果c是大寫字符,則返回其小寫字母,否則直接返回c

    toupper(c)  跟tolower相反

看一個鞏固練習代碼:

  1. #include <iostream>  
  2. #include <string>  
  3. #include <cctype>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.     string str1="Hello World!!!";  
  8.     string::size_type punct_cnt = 0;  
  9.     for(string::size_type i=0;i!=str1.size();++i)  
  10.     {  
  11.         if(ispunct(str1[i]))  
  12.             ++punct_cnt;  
  13.         str1[i]=toupper(str1[i]);  
  14.     }  
  15.     cout<<"字符中標點符號有:"<<punct_cnt<<endl;  
  16.     cout<<str1<<endl;  
  17.     return 0;  
  18. }  

4:string對象中一些函數

/*-------------------------插入函數----------------------------------包括迭代器操作和下標操作,下標操作更靈活*/

s.insert( it , p );  把字符串p插入到it的位置

s.insert(p,n,t);   迭代器p元素之前插入n個t的副本

s.insert(p,b,e);      迭代器p元素之前插入迭代器b到e之間的所有元素

s.insert(p,s2,poe2,len); 在下標p之前插入s2下標從poe2開始長度爲len的元素

s.insert(pos,cp,len);  下標pos之前插入cp數組的前len個元素。

/*-----------------------替換函數-------------------------------*/

s.assign(b,e);  用迭代器b到e範圍內的元素替換s

s.assign(n,t);  用n個t的副本替換s

a.assign(s1,pos2,len);從s1的下標pos2開始連續替換len個。

s.replace ( 3 , 3 , " good " ) ;   從第三個起連續三個替換爲good

s.substr(i,j)   截取s串中從i到j的子串  //string::npos  判斷字符串是否結束

/*-----------------------刪除函數-----------------------------*/

s.erase( 3 )||s.erase ( 0 , 4 ) ;  刪除第四個元素或第一到第五個元素

/*----------------------其他函數-----------------------------*/

s.find ( " cat " ) ;  超找第一個出現的字符串”cat“,返回其下標值,查不到返回 4294967295,也可查找字符;

s.append(args); 將args接到s的後面

s.compare ( " good " ) ;  s與”good“比較相等返回0,比"good"大返回1,小則返回-1;

reverse ( s.begin(), s.end () );  反向排序函數,即字符串反轉函數


下面看一些鞏固練習:

  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <string>  
  4. #include <numeric>  
  5. using namespace std;  
  6. int main(int argc,char *argv[])  
  7. {  
  8.     string s;  
  9.     s="54268713";  
  10.     reverse(s.begin(),s.end()); //字符串反轉  
  11.     cout<<s<<endl;  
  12.   
  13.     string s1="i love you";  
  14.     string::iterator it;  
  15.     it=s1.begin();  
  16.     s1.insert(it+1,'p');  //插入  
  17.     cout<<s1<<endl;  
  18.   
  19.     string s2("abc123456");  
  20.     string::iterator it2=s2.begin();  
  21.     s2.erase(it2+6);  //刪除  
  22.     cout<<s2<<endl;  
  23.     s2.erase(it2,it2+3);  
  24.     cout<<s2<<endl;  
  25.     s2.replace(2,1,"good");  //替換  
  26.     cout<<s2<<endl;  
  27.     cout<<s2.find("good")<<endl;  //搜索返回下標值  
  28.     cout<<s2.compare("12good56")<<endl;  //比較,自行修改值看其返回值  
  29.     cout<<s2.compare("12good56758")<<endl;  
  30.   
  31.     return 0;  
  32. }  

5:string的一些常用操作及用法

***string對象作爲vector元素

***string對象的數字化處理

***string對象與sscanf函數

直接代碼:

  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <string>  
  4. #include <numeric>  
  5. #include <vector>  
  6. #include <cstdio>  
  7. using namespace std;  
  8. int main(int argc,char *argv[])  
  9. {  
  10.     vector<string> v;   //vector的string  
  11.     v.push_back("Iack");  
  12.     v.push_back("Mike");  
  13.     v.push_back("Tom cluce");  
  14.     cout<<v[0]<<endl;  
  15.     cout<<v[1][1]<<endl;  
  16.     cout<<v[2].size()<<endl;  
  17.   
  18.     char s3[100],s2[100];  
  19.     string str3,str2;  
  20.     int ab,ac,ad;  
  21.     sscanf("abc fsaf","%s %s",s2,s3);  //注意string不能直接用於sscanf  
  22.     str3=s3;str2=s2;  
  23.     cout<<str3<<" "<<str2<<endl;  
  24.     sscanf("4,5$10000","%d,%d$%d",&ab,&ac,&ad);  
  25.     cout<<ab<<" "<<ac<<" "<<ad<<endl;  
  26.   
  27.     char s[200];  
  28.     cin>>s;  
  29.     cin>>s;  
  30.     string s1=s;  
  31.     printf(s1.c_str());  //c輸出字符串對象  
  32.   
  33.     return 0;  
  34. }  

6:string與數值的相互轉換

注意下面c++的兩個轉化函數,比較好用,也比較常用、

  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <string>  
  4. #include <numeric>  
  5. #include <vector>  
  6. #include <cstdio>  
  7. #include <sstream>  
  8. using namespace std;  
  9.   
  10. //c++方法:將數值轉換爲string  
  11. string convert_to_string(double x)  
  12. {  
  13.     ostringstream o;  
  14.     if(o << x)  
  15.         return o.str();  
  16.     return "conversion error";  
  17. }  
  18. //c++方法,將string轉化爲數值  
  19. double convert_from_string(const string &s)  
  20. {  
  21.     istringstream i(s);  
  22.     double x;  
  23.     if(i >> x)  
  24.         return x;  
  25.     return 0.0;  
  26. }  
  27. int main(int argc,char *argv[])  
  28. {  
  29.     //將數值轉換爲string的第一種方法:c方法  
  30.     char b[10];  
  31.     string a;  
  32.     sprintf(b,"%d",1975);  //數值轉化爲string  
  33.     a=b;  
  34.     cout<<a<<endl;  
  35.   
  36.     string cc=convert_to_string(1976);  
  37.     cout<<cc<<endl;  
  38.   
  39.     string dd="115165";  
  40.     int p=convert_from_string(dd)+2;  
  41.     cout<<p<<endl;  
  42.     return 0;  
  43. }  

下面推薦一些字符串的題目
hdoj 2017 字符串中統計數字,直接調用上面s.digit()函數
hdoj 1020  判斷輸出重複、水題、
hdoj 1062 逆轉字符串 注意1:getchar()吸收3後'\n',2:空格不止有一個
hdoj 1039,字符串處理,清晰思路,可以寫三個判斷條件的3個函數,調用函數判斷,思路清晰,容易判斷;
hdoj 1088 對字符串按一個一個處理。一次性輸入一行不好控制
hdoj 1113 map容器+字典序。值得做
hdoj 1161 tolower() 函數轉化爲小寫就ok
1200、1251、1256、1288、1321、1328、1379、1804、1860、 1982、1984、2017、2024、2025、2026、2027、2043、2052、2054、2072、2074、2087、2131、 2137、2140、2163、2203、2206、2352、2500、2549、2564、2565、2567、2572、2609、2607、 2707、2708、2719、2721、2723、

比較詳細,希望幫助到了跟我一樣正在學習中的菜鳥、、、



文章摘自 : http://blog.csdn.net/y990041769/article/details/8763366

發佈了18 篇原創文章 · 獲贊 322 · 訪問量 46萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章