【C++ Primer】标准库string类型
来源:互联网 发布:怎样与淘宝买家联系 编辑:程序博客网 时间:2024/06/16 23:58
/**标准库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;}