在學習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
下面看一個鞏固練習:
?
[cpp] view plaincopyprint?
#include <iostream>? #include <string>? using namespace std;? int main()? {? ??? string s1;? ??? s1="i love you";? ??? string s2(s1);? ??? string s3("value");? ??? string s4(10,'s');?? ??? ??? cout<<s2<<" "<<s3<<" "<<s4<<endl;? ??? string s5;? ??? while(cin>>s5)? ??? {? ??????? cout<<s5<<endl;? ??? }? ??? return 0;? }? #include <iostream>
#include <string>
using namespace std;
int main()
{string s1;s1="i love you";string s2(s1); //把s2初始化為s1的一個副本,注意寫法,不能前面先定義s2的類型后面直接寫,也不能定義兩次s2string s3("value"); //將s3初始化一個字符串面值副本string s4(10,'s'); //將s4初始化為字符‘s'的10個副本/*注意字符串面值與標準庫string不是同一個類型*/cout<<s2<<" "<<s3<<" "<<s4<<endl;string s5;while(cin>>s5) //這里可以輸入“ hello world ”測試,發現只讀取有效字符到遇到空格結束{cout<<s5<<endl;}return 0;
}
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
!=,<,<=,>,>=? 字符串比較,兩個字符串短的與長的前面匹配,短的小于長的
鞏固練習:
?
[cpp] view plaincopyprint?
#include <iostream>? #include <string>? using namespace std;? int main()? {? ??? string str1;? ??? string str2("the size of ");? ??? string str3=" hello world? "; ??? str3+=str2;? ??? str3.append("haha secessful");? ??? cout<<str3<<endl;? ??? cout<<"the size of is "<<str2.size()<<endl;? ??? ??? getline(cin,str1);? ??? while(!str1.empty())? ??? {? ??????? for(string::size_type i=0;i!=str1.size();++i)? ??????? {? ??????????? cout<<str1[i];? ??????? }? ??????? cout<<endl;break;? ??? }? ??? return 0;? }? #include <iostream>
#include <string>
using namespace std;
int main()
{string str1;string str2("the size of ");string str3=" hello world ";//空格不會忽略str3+=str2;str3.append("haha secessful");cout<<str3<<endl;cout<<"the size of is "<<str2.size()<<endl;/*注意這里取長度的str2.size(),和str2.length(),但是注意str2.size()返回的值并不是int類型,事實表明size_type存儲的string長度是int所能存儲的兩倍*/getline(cin,str1); //read line at time until end-of-file,注意寫法。while(!str1.empty()) //返回一個bool值,空的話返回true,否則返回false。{for(string::size_type i=0;i!=str1.size();++i) //注意size_type類型{cout<<str1[i];}cout<<endl;break;}return 0;
}
?
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相反
看一個鞏固練習代碼:
?
[cpp] view plaincopyprint?
#include <iostream>? #include <string>? #include <cctype>? using namespace std;? int main()? {? ??? string str1="Hello World!!!";? ??? string::size_type punct_cnt = 0;? ??? for(string::size_type i=0;i!=str1.size();++i)? ??? {? ??????? if(ispunct(str1[i]))? ??????????? ++punct_cnt;? ??????? str1[i]=toupper(str1[i]);? ??? }? ??? cout<<"字符中標點符號有:"<<punct_cnt<<endl;? ??? cout<<str1<<endl;? ??? return 0;? }? #include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{string str1="Hello World!!!";string::size_type punct_cnt = 0;for(string::size_type i=0;i!=str1.size();++i){if(ispunct(str1[i]))++punct_cnt;str1[i]=toupper(str1[i]);}cout<<"字符中標點符號有:"<<punct_cnt<<endl;cout<<str1<<endl;return 0;
}
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 () );? 反向排序函數,即字符串反轉函數
?
下面看一些鞏固練習:
?
[cpp] view plaincopyprint?
#include <iostream>? #include <algorithm>? #include <string>? #include <numeric>? using namespace std;? int main(int argc,char *argv[])? {? ??? string s;? ??? s="54268713";? ??? reverse(s.begin(),s.end()); ??? cout<<s<<endl;? ? ??? string s1="i love you";? ??? string::iterator it;? ??? it=s1.begin();? ??? s1.insert(it+1,'p');? ??? cout<<s1<<endl;? ? ??? string s2("abc123456");? ??? string::iterator it2=s2.begin();? ??? s2.erase(it2+6);? ??? cout<<s2<<endl;? ??? s2.erase(it2,it2+3);? ??? cout<<s2<<endl;? ??? s2.replace(2,1,"good");? ??? cout<<s2<<endl;? ??? cout<<s2.find("good")<<endl;? ??? cout<<s2.compare("12good56")<<endl;? ??? cout<<s2.compare("12good56758")<<endl;? ? ??? return 0;? }? #include <iostream>
#include <algorithm>
#include <string>
#include <numeric>
using namespace std;
int main(int argc,char *argv[])
{string s;s="54268713";reverse(s.begin(),s.end()); //字符串反轉cout<<s<<endl;string s1="i love you";string::iterator it;it=s1.begin();s1.insert(it+1,'p'); //插入cout<<s1<<endl;string s2("abc123456");string::iterator it2=s2.begin();s2.erase(it2+6); //刪除cout<<s2<<endl;s2.erase(it2,it2+3);cout<<s2<<endl;s2.replace(2,1,"good"); //替換cout<<s2<<endl;cout<<s2.find("good")<<endl; //搜索返回下標值cout<<s2.compare("12good56")<<endl; //比較,自行修改值看其返回值cout<<s2.compare("12good56758")<<endl;return 0;
}
5:string的一些常用操作及用法
***string對象作為vector元素
***string對象的數字化處理
***string對象與sscanf函數
直接代碼:
?
[cpp] view plaincopyprint?
#include <iostream>? #include <algorithm>? #include <string>? #include <numeric>? #include <vector>? #include <cstdio>? using namespace std;? int main(int argc,char *argv[])? {? ??? vector<string> v;?? ??? v.push_back("Iack");? ??? v.push_back("Mike");? ??? v.push_back("Tom cluce");? ??? cout<<v[0]<<endl;? ??? cout<<v[1][1]<<endl;? ??? cout<<v[2].size()<<endl;? ? ??? char s3[100],s2[100];? ??? string str3,str2;? ??? int ab,ac,ad;? ??? sscanf("abc fsaf","%s %s",s2,s3);? ??? str3=s3;str2=s2;? ??? cout<<str3<<" "<<str2<<endl;? ??? sscanf("4,5$10000","%d,%d$%d",&ab,&ac,&ad);? ??? cout<<ab<<" "<<ac<<" "<<ad<<endl;? ? ??? char s[200];? ??? cin>>s;? ??? cin>>s;? ??? string s1=s;? ??? printf(s1.c_str());? ? ??? return 0;? }? #include <iostream>
#include <algorithm>
#include <string>
#include <numeric>
#include <vector>
#include <cstdio>
using namespace std;
int main(int argc,char *argv[])
{vector<string> v; //vector的stringv.push_back("Iack");v.push_back("Mike");v.push_back("Tom cluce");cout<<v[0]<<endl;cout<<v[1][1]<<endl;cout<<v[2].size()<<endl;char s3[100],s2[100];string str3,str2;int ab,ac,ad;sscanf("abc fsaf","%s %s",s2,s3); //注意string不能直接用于sscanfstr3=s3;str2=s2;cout<<str3<<" "<<str2<<endl;sscanf("4,5$10000","%d,%d$%d",&ab,&ac,&ad);cout<<ab<<" "<<ac<<" "<<ad<<endl;char s[200];cin>>s;cin>>s;string s1=s;printf(s1.c_str()); //c輸出字符串對象return 0;
}
6:string與數值的相互轉換
注意下面c++的兩個轉化函數,比較好用,也比較常用、
?
[cpp] view plaincopyprint?
#include <iostream>? #include <algorithm>? #include <string>? #include <numeric>? #include <vector>? #include <cstdio>? #include <sstream>? using namespace std;? ? string convert_to_string(double x)? {? ??? ostringstream o;? ??? if(o << x)? ??????? return o.str();? ??? return "conversion error";? }? double convert_from_string(const string &s)? {? ??? istringstream i(s);? ??? double x;? ??? if(i >> x)? ??????? return x;? ??? return 0.0;? }? int main(int argc,char *argv[])? {? ??? ??? char b[10];? ??? string a;? ??? sprintf(b,"%d",1975);? ??? a=b;? ??? cout<<a<<endl;? ? ??? string cc=convert_to_string(1976);? ??? cout<<cc<<endl;? ? ??? string dd="115165";? ??? int p=convert_from_string(dd)+2;? ??? cout<<p<<endl;? ??? return 0;? }? #include <iostream>
#include <algorithm>
#include <string>
#include <numeric>
#include <vector>
#include <cstdio>
#include <sstream>
using namespace std;//c++方法:將數值轉換為string
string convert_to_string(double x)
{ostringstream o;if(o << x)return o.str();return "conversion error";
}
//c++方法,將string轉化為數值
double convert_from_string(const string &s)
{istringstream i(s);double x;if(i >> x)return x;return 0.0;
}
int main(int argc,char *argv[])
{//將數值轉換為string的第一種方法:c方法char b[10];string a;sprintf(b,"%d",1975); //數值轉化為stringa=b;cout<<a<<endl;string cc=convert_to_string(1976);cout<<cc<<endl;string dd="115165";int p=convert_from_string(dd)+2;cout<<p<<endl;return 0;
}
下面推薦一些字符串的題目 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、
比較詳細,希望幫助到了跟我一樣正在學習中的菜鳥、、、
總結
以上是生活随笔 為你收集整理的C++STL之string (转) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。