【C++ Primer】標準庫string型別
阿新 • • 發佈:2019-01-23
/* *標準庫string型別 *Zhi-Yun Deng *2013-11-01 */ #include <iostream> #include <string> //標準庫string型別標頭檔案 #include <cctype> //string物件中字元的處理函式的標頭檔案 using std::string; //string型別的using宣告 using std::cin; using std::cout; using std::endl; int main() { int n = 4; string s1; //預設建構函式,s1為空串 string s2(s1); //將s2初始化為s1的一個副本 string s3("value"); //將s3初始化為一個字串字面值副本 string s4(n, 'c'); //將s4初始化為字元‘c’的n個副本 cout << "1、string物件的定義和初始化" << endl; cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; cout << "s3: " << s3 << endl; cout << "s4: " << s4 << endl; cout << "****************************************\n" << endl; cout << "2、string物件的讀寫" << endl; string word; while(cin >> word) { cout << "讀入未知數目的string物件,word:" << word << endl; if(word == "break") break; } string line; while(getline(cin, line)) { cout << "用getline讀取整行文字,line:" << line << endl; if(line == "break") break; } cout << "****************************************\n" << endl; cout << "3、string物件的操作" << endl; string st("The expense of spirit\n"); cout << "The size of " << st << "is " << st.size() << " characters, including the newline" << endl; if(!st.size()==0) cout << st.size() << endl; if(!st.empty()) cout << st.size() << endl; s2 = s3; cout << "string物件的賦值,s2=s3,s2:" << s2 << endl; s1 = "hello"; s2 = "world"; s3 = s1 + ", "; s4 = s1 + ", " + "world"; cout << "s1: " << s1 << "\t size: "<< s1.size() << endl; cout << "s2: " << s2 << "\t size: "<< s2.size() << endl; cout << "s3: " << s3 << "\t size: "<< s3.size() << endl; cout << "s4: " << s4 << "\t size: "<< s4.size() << endl; for(string::size_type ix=0; ix!=s4.size(); ++ix) cout << s4[ix] << endl; for(string::size_type ix=0; ix!=s4.size(); ++ix) s4[ix]='*'; cout << "s4: " << s4 << "\t size: "<< s4.size() << endl; cout << "****************************************\n" << endl; cout << "4、string物件中字元的處理" << endl; string testStr("123abcABC *&^%$#@()-+=,.;'?/\n\t\r!~"); string::size_type alnum_cnt(0); //統計字母或數字的個數 string::size_type alpha_cnt(0); //統計字母的個數 string::size_type digit_cnt(0); //統計數字的個數 string::size_type print_cnt(0); //統計可列印字元的個數 string::size_type graph_cnt(0); //統計除空格外可列印的字元的個數 string::size_type space_cnt(0); //統計空白字元的個數 string::size_type lower_cnt(0); //統計小寫字母的個數 string::size_type upper_cnt(0); //統計大寫字母的個數 string::size_type punct_cnt(0); //統計標點符號的個數 string::size_type cntrl_cnt(0); //統計控制字元的個數 string::size_type xdigit_cnt(0); //統計十六進位制數的個數 cout << "testStr的字元個數:" << testStr.size() << endl; for(string::size_type index=0; index!=testStr.size(); ++index) { if(isalnum(testStr[index])) ++alnum_cnt; if(isalpha(testStr[index])) ++alpha_cnt; if(isdigit(testStr[index])) ++digit_cnt; if(isprint(testStr[index])) ++print_cnt; if(isgraph(testStr[index])) ++graph_cnt; if(isspace(testStr[index])) ++space_cnt; if(islower(testStr[index])) ++lower_cnt; if(isupper(testStr[index])) ++upper_cnt; if(ispunct(testStr[index])) ++punct_cnt; if(iscntrl(testStr[index])) ++cntrl_cnt; if(isxdigit(testStr[index])) { cout << testStr[index] << endl; ++xdigit_cnt; } } cout << "alnum_cnt: " << alnum_cnt << endl; cout << "alpha_cnt: " << alpha_cnt << endl; cout << "digit_cnt: " << digit_cnt << endl; cout << "print_cnt: " << print_cnt << endl; cout << "graph_cnt: " << graph_cnt << endl; cout << "space_cnt: " << space_cnt << endl; cout << "lower_cnt: " << lower_cnt << endl; cout << "upper_cnt: " << upper_cnt << endl; cout << "punct_cnt: " << punct_cnt << endl; cout << "cntrl_cnt: " << cntrl_cnt << endl; cout << "xdigit_cnt: " << xdigit_cnt << endl; for(string::size_type index=0; index != testStr.size(); ++index) testStr[index] = tolower(testStr[index]); cout << "tolower: " << testStr << endl; for(string::size_type index=0; index != testStr.size(); ++index) testStr[index] = toupper(testStr[index]); cout << "toupper: " << testStr << endl; cout << "****************************************\n" << endl; return 0; }