#include<iostream> using namespace std; #include<string> int main() { //處理每個(gè)字符,范圍for string s1 = "aigjiouarjiu"; for (auto c : s1) { int i = 1; cout<<"s1字符串的第"<<i<<"個(gè)字符:"<<c<<endl; } string s2 = "sg,gg.gga,,,,!!!!rgq/q/gq'qgr???"; decltype (s2.size()) punt=0; //decltype (s1.size(),返回一個(gè)數(shù)據(jù)類型 for (auto c : s2) { if (ispunct(c)) { punt++; } } cout << "s2中有" << punt << "個(gè)標(biāo)點(diǎn)符號(hào)" << endl; //使用范圍for改變字符串的值,聲明部分為引用 string s3 = "huang jun mu"; for (auto &c : s3) { c = toupper(c); } cout << "s3:" << s3 << endl; //只處理一部分字符 //下標(biāo)運(yùn)算符 string s4 = "huang junmu"; if (!s4.empty()) { s4[0] = toupper(s4[0]); } cout << "s4: " << s4 << endl; //使用下標(biāo)進(jìn)行迭代 for (decltype(s4.size()) index = 0; index != s4.size() && !isspace(s4[index]); ++index) { s4[index] = toupper(s4[index]); } cout << "s4: " << s4 << endl; //使用下標(biāo)執(zhí)行隨機(jī)訪問 system("pause"); }
|
|
來(lái)自: 木俊 > 《c primer》