1.字符串相關處理
注意包含cctype頭文件
下面是詳細代碼
string str("123abdaDFea789E,. ;;, p!#");
string str1("123abcdEFG");
//輸出字符串裏的數字和字母
for (auto c : str)
{//c爲str裏的字符,自動加一
if (isalnum(c))
cout << c;
}
cout << endl;
//輸出字符串裏的字母
for (auto c : str)
{//c爲str裏的字符,自動加一
if (isalpha(c))
cout << c;
}
cout << endl;
//輸出字符串裏的數字
for (auto c : str)
{//c爲str裏的字符,自動加一
if (isdigit(c))
cout << c;
}
cout << endl;
//輸出字符串裏的小寫字母
for (auto c : str1)
{//c爲str裏的字符,自動加一
if (islower(c))
cout << c;
}
cout << endl;
//字符串裏的大寫轉小寫
for (auto &c : str1)
{//c爲str裏的字符,自動加一
c = tolower(c);
cout << c;
}
cout << endl;
2.類模板vector
(1)定義
注意括號和花括號在初始化的區別,整數vector<int> n(10, 1);是表示初始化爲10個1,字符串 vector<string> n2{ 10, "1" };用的是花括號初始化爲10個字符串“1”.
vector<int> n(10, 1);
vector<string> n1{10, "1" };
vector<string> n2{ 10, "1" };
vector<string> n3{ 10 };
vector<string> n4{ "hi" };
(2)基本操作