C++ string類簡介(附小段程式碼案例)

關鍵字:C++, string類, 容器,迭代器,erase

筆者在之前的文章裡介紹了C++ STL的容器、迭代器和演算法的概念。下面再介紹一個C++裡非常實用,而且用起來很像基本資料型別的string類。

1 string簡介

string類是C++標準庫的模板類。

它實際上是從STL的標準模板類basic_string透過typedef來的。

經過typedef之後,用起來更方便一些,

使得string就像一個基本的資料型別。

typedef basic_string string;

string類功能很強大,非常有用。

學會string之後就不太需要純C語言的char *來處理字串了。

2 string的常用的功能

string的功能很好很強大,這裡列一些筆者認為的常用的功能:

string 物件的長度用成員函式 length() 讀取;

string s(“hello world”);cout << s。length() << endl;

string 支援流讀取運算子

string stringObject;cin >> stringObject;

string 支援 getline 函式

string s;getline(cin ,s);

strign支援用 = 賦值

string s1(“tiger”), s2;s2 = s1;

string用 + 運算子連線字串

string s1(“Ni how ”), s2(“are you! ”);s1 += s2;cout << s1;

用關係運算符比較 string 的大小

== , >, >=, <, <=,

返回值都是 bool 型別,成立返回 true, 否則返回 false

string的成員函式 swap

string s1(“Ni how are you”), s2(“Nice to meet you”);s1。swap(s2);

string的成員函式 find ()

string s1(“hello world”);s1。 find (“wor”);

在 s1 中從前向後查詢 wor 第一次出現的地方,

如果找到,返回 wor開始的位置,即 w 所在的位置 下標 。

如果找不到,返回string::npos (string 中定義的靜態常量)

string的成員函式 erase

string s1(“hello worlld”);s1。erase(5);//去掉下標 5 及之後的字元

string的成員函式 c_str

string s1(“hello world”);printf(“%s\n”, s1。 c_str()) ;

s1。c_str()返回傳統的

const char *

型別字串,

且該字串以‘ \0 ’結尾。

string成員函式 data

string s1(“hello world”);char * p1=s1。data() ;for(int i=0;i

s1。data()返回一個

char *

型別的字串,

對 s1 的修改可能會使 p1 出錯。

3 string程式小案例說明

筆者用string寫了一個類似Linux命令uniq的小程式。

功能是檢查檔案中每一行的內容,

如果

相鄰

若干行的內容相同,則只保留一行。

把檔案的每一行用getline取出來,放在string中;

然後儲存在vector型別的容器中;

容器的迭代器為ii;

用 *ii 是否等於* (ii+1)來判斷相鄰的2行內容是否相同;

用vector容器的成員函式erase,結合迭代器,來刪除相鄰的內容相同的行。

注意:迭代器ii迴圈的時候,只能到容器的end -1個元素

如果迴圈到end,

會導致 * (ii+1) 指標越界,

程式能透過編譯,但執行的時候會crash。

其他細節請看原始碼,我加了很多註釋

4 程式原始碼:

#include #include #include #include #include using namespace std;int main (int argc, char* argv[]){ // usage: myuniq -help // usage: myuniq file_name if (argc > 2 ) { cout << “Wrong input option number:” << argc -1 << endl; cout << “Please input:myuniq file_name” << endl; return 0 ; } // check -help string option_1 = argv[1] ; if (option_1 == “-help” ) { cout << “ usage: myuniq file_name ” << endl ; return 0 ; } // open file // use argv[1] directly, cannot use string, // because ifstream input shoud be char * ifstream input_f(argv[1]) ; // input argv 1 if ( !input_f ) { // check if input file can be opened cout << “Error: the input file can not be opened” < all_lines ; // 定義容器container to store all lines while ( getline(input_f, i_line) ) { // get each line all_lines。push_back(i_line); } vector::iterator ii ; //定義迭代器 for( ii = all_lines。begin() ; ii < (all_lines。end() - 1) ; ++ii ) { while ( *ii == * (ii + 1) ) { //用 *迭代器 來獲得相鄰行的內容,並做string ==比較 all_lines。erase(ii+1) ; //用vector的成員函式erase來刪除重複的行 } cout << *ii << endl; //輸出 } input_f。close() ; return 0 ;}

5 程式小測試

測試檔案test。txt:

12 24 45 9 48 512 24 45 9 48 512 24 45 9 48 512 24 45 9 48 512 24 65 c 48 512 24 45 b 48 512 24 45 a 48 512 24 45 9 48 512 24 75 f 48 512 24 75 f 48 512 24 75 f 48 512 24 75 f 48 512 24 45 d 48 512 24 95 9 48 512 24 45 9 48 512 24 35 9 48 512 24 25 9 48 512 24 25 9 48 5

測試結果:

g++ file_uniq。cpp -o myuniq。/myuniq test。txt12 24 45 9 48 512 24 45 9 48 512 24 65 c 48 512 24 45 b 48 512 24 45 a 48 512 24 45 9 48 512 24 75 f 48 512 24 45 d 48 512 24 95 9 48 512 24 45 9 48 512 24 35 9 48 512 24 25 9 48 5