Welcome to Leon's Blog |
|
|||
日歷
統計
導航常用鏈接留言簿(4)隨筆分類隨筆檔案
ACM搜索最新評論
閱讀排行榜評論排行榜 |
之所以拋棄char*的字符串而選用C++標準程序庫中的string類,是因為他和前者比較起來,不必擔心內存是否足夠、字符串長度等等,而且作
為一個類出現,他集成的操作函數足以完成我們大多數情況下(甚至是100%)的需要。我們可以用 = 進行賦值操作,== 進行比較,+
做串聯(是不是很簡單?)。我們盡可以把它看成是C++的基本數據類型。 字符串操作是一個不小的主題,在標準C++中,string字符串類成為一個標準,之所以拋棄char*的字符串而選用C++標準程序庫中的string類,是因為他和前者比較起來,不必擔心內存是否足夠、字符串長度等等,而且作為一個類出現,他集成的操作函數足以完成我們大多數情況下的需要. 下面我們首先從一些示例開始學習下string類的使用. 1) #include <string> #include <iostream> using namespace std; void main() { string s("hehe"); cout<<s<<endl; cin.get(); } 2) #include <string> #include <iostream> using namespace std; void main() { char chs[] = "hehe"; string s(chs); cout<<s<<endl; cin.get(); } 3) #include <string> #include <iostream> using namespace std; void main() { char chs[] = "hehe"; string s(chs,1,3); //指定從chs的索引1開始,最后復制3個字節 cout<<s<<endl; cin.get(); } 4) #include <string> #include <iostream> using namespace std; void main() { string s1("hehe"); string s2(s1); cout<<s2<<endl; cin.get(); } 5) #include <string> #include <iostream> using namespace std; void main() { string s1("hehe",2,3); string s2(s1); cout<<s2<<endl; cin.get(); } 6) #include <string> #include <iostream> using namespace std; void main() { char chs[] = "hehe"; string s(chs,3); //將chs前3個字符作為初值構造 cout<<s<<endl; cin.get(); } 7) #include <string> #include <iostream> using namespace std; void main() { string s(10,'k'); //分配10個字符,初值都是'k' cout<<s<<endl; cin.get(); } //以上是string類實例的構造手段,都很簡單. 9) //賦新值 #include <string> #include <iostream> using namespace std; void main() { string s(10,'k'); //分配10個字符,初值都是'k' cout<<s<<endl; s = "hehehehe"; cout<<s<<endl; s.assign("kdje"); cout<<s<<endl; s.assign("fkdhfkdfd",5); //重新分配指定字符串的前5的元素內容 cout<<s<<endl; cin.get(); } 10) //swap方法交換 #include <string> #include <iostream> using namespace std; void main() { string s1 = "hehe"; string s2 = "gagaga"; cout<<"s1 : "<<s1<<endl; cout<<"s2 : "<<s2<<endl; s1.swap(s2); cout<<"s1 : "<<s1<<endl; cout<<"s2 : "<<s2<<endl; cin.get(); } 11) //+=,append(),push_back()在尾部添加字符 #include <string> #include <iostream> using namespace std; void main() { string s = "hehe"; s += "gaga"; cout<<s<<endl; s.append("嘿嘿"); //append()方法可以添加字符串 cout<<s<<endl; s.push_back('k'); //push_back()方法只能添加一個字符... cout<<s<<endl; cin.get(); } 12) //insert() 插入字符.其實,insert運用好,與其他的插入操作是一樣的. #include <string> #include <iostream> using namespace std; void main() { string s = "hehe"; s.insert(0,"頭部"); //在頭部插入 s.insert(s.size(),"尾部"); //在尾部插入 s.insert(s.size()/2,"中間");//在中間插入 cout<<s<<endl; cin.get(); } 13) #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg"; s.erase(0,1); //從索引0到索引1,即刪除掉了'a' cout<<s<<endl; //其實,還可以使用replace方法來執行刪除操作 s.replace(2,3,"");//即將指定范圍內的字符替換成"",即變相刪除了 cout<<s<<endl; cin.get(); } 14) //clear() 刪除全部字符 #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg"; cout<<s.length()<<endl; s.clear(); cout<<s.length()<<endl; //使用earse方法變相全刪除 s = "dkjfd"; cout<<s.length()<<endl; s.erase(0,s.length()); cout<<s.length()<<endl; cin.get(); } 15) //replace() 替換字符 #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg"; s.replace(2,3,"!!!!!");//從索引2開始3個字節的字符全替換成"!!!!!" cout<<s<<endl; cin.get(); } 16) //==,!=,<,<=,>,>=,compare() 比較字符串 #include <string> #include <iostream> using namespace std; void main() { string s1 = "abcdefg"; string s2 = "abcdefg"; if (s1==s2)cout<<"s1 == s2"<<endl; else cout<<"s1 != s2"<<endl; if (s1!=s2)cout<<"s1 != s2"<<endl; else cout<<"s1 == s2"<<endl; if (s1>s2)cout<<"s1 > s2"<<endl; else cout<<"s1 <= s2"<<endl; if (s1<=s2)cout<<"s1 <= s2"<<endl; else cout<<"s1 > s2"<<endl; cin.get(); } 17) //size(),length() 返回字符數量 #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg"; cout<<s.size()<<endl; cout<<s.length()<<endl; cin.get(); } 18) //max_size() 返回字符的可能最大個數 #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg"; cout<<s.max_size()<<endl; cin.get(); } 19) //empty() 判斷字符串是否為空 #include <string> #include <iostream> using namespace std; void main() { string s ; if (s.empty()) cout<<"s 為空."<<endl; else cout<<"s 不為空."<<endl; s = s + "abcdefg"; if (s.empty()) cout<<"s 為空."<<endl; else cout<<"s 不為空."<<endl; cin.get(); } 20) // [ ], at() 存取單一字符 #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg1111"; cout<<"use []:"<<endl; for(int i=0; i<s.length(); i++) { cout<<s[i]<<endl; } cout<<endl; cout<<"use at():"<<endl; for(int i=0; i<s.length(); i++) { cout<<s.at(i)<<endl; } cout<<endl; cin.get(); } 21) #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg1111"; const char * chs1 = s.c_str(); const char * chs2 = s.data(); cout<<"use at():"<<endl; int i; for(i=0; i<s.length(); i++) { cout<<"c_str() : "<<chs1[i]<<endl; cout<<"data() : "<<chs2[i]<<endl; } cout<<"c_str() : "<<chs1<<endl; cout<<"data() : "<<chs2<<endl; cout<<endl; cin.get(); } 22) // substr() 返回某個子字符串 #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg1111"; string str = s.substr(5,3);//從索引5開始3個字節 cout<<str<<endl; cin.get(); } 23) // find 查找函數 #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg1111"; string pattern = "fg"; string::size_type pos; pos = s.find(pattern,0); //從索引0開始,查找符合字符串"f"的頭索引 cout<<pos<<endl; string str = s.substr(pos,pattern.size()); cout<<str<<endl; cin.get(); } 24) // begin() end() 提供類似STL的迭代器支持 #include <string> #include <iostream> using namespace std; void main() { string s = "abcdefg1111"; for(string::iterator iter = s.begin(); iter!=s.end(); iter++) { cout<<*iter<<endl; } cout<<endl; cin.get(); } 一個C++字符串存在三種大小:a)現有的字符數,函數是size()和length(),他們等效。 Empty()用來檢查字符串是否為空。b)max_size() 這個大小是指當前C++字符串最多能包含的字符數,很可能和機器本身的限制或者字符串所在位置連續內存的大小有關系。我們一般情況下不用關心他,應該大小 足夠我們用的。但是不夠用的話,會拋出length_error異常c)capacity()重新分配內存之前 string所能包含的最大字符數。這里另一個需要指出的是reserve()函數,這個函數為string重新分配內存。重新分配的大小由其參數決定, 默認參數為0,這時候會對string進行非強制性縮減
評論:
|
![]() |
|
Copyright © Leon916 | Powered by: 博客園 模板提供:滬江博客 |