标准库类型string的基本功能和使用
標準庫類型string的基本功能和使用
- String基本知識
- 初始化
- 基本操作
- 范圍for語句
- string定義的迭代器及其他
- 關(guān)于下標操作
- 示例代碼
- 練習3.6
- cctype.cpp
- punctRemove.cpp
- char_repl.cpp
本文是本人大一期間在校學習C++課程時所撰寫的實驗報告的摘錄,由于剛上大學,剛接觸計算機編程方面的相關(guān)知識,故可能會有很多不足甚至錯誤的地方,還請各位看官指出及糾正。
本文所涉及到的“教材”指:
電子工業(yè)出版社《C++ Primary中文版(第5版)》
如需轉(zhuǎn)載或引用請標明出處。
String基本知識
標準庫類型string表示可變長的字符序列,其類型與C語言中的字符串字面值的類型是不同的。與C語言管用的用字符數(shù)組表示字符串相比,string的一大特點是其可以改變長度,更有靈活性。
初始化
初始化string對象的方法有很多:
string str1
表示默認初始化,str1是一個空字符串
string str2 = str1
表示str2是str1的副本
string str3 = “value”
表示str3是字面值”value”的副本,不包括最后的空字符
string str4(n, ‘c’)
表示把str4初始化為由連續(xù)n個字符c組成的串
更多初始化string對象的方法詳見教材第76頁
基本操作
string包括了一些便捷的成員函數(shù),如size、empty等。同時string也支持很多便捷的操作,如使用+直接返回兩個string相連后的結(jié)果,使用==、<、>=等直接比較兩個string的大小等。具體的string操作在教材第77頁有詳細的表格,這里就不再復(fù)述了。這里要注意的是getline函數(shù)是從輸入中讀取一整行,與cin >> str是不一樣的,后者只從第一個非空白字符讀取到下一個空白字符前,再返回給str。
處理string對象中元素的方法有很多,包括了for循環(huán)、范圍for循環(huán)、while循環(huán)、string定義的迭代器、指針等。配合上cctype頭文件中的函數(shù),可以很方便地處理string對象中的元素。
下面是cctype頭文件中包含的一些函數(shù)的簡單介紹:
isalnum( c )
當c是字母或數(shù)字時為真
isalpha( c )
當c是字母時為真
islower( c )
當c是小寫字母時為真
toupper( c )
若c時小寫字母,則輸出相應(yīng)的大寫字母;否則原樣輸出c
更多cctype頭文件包含的函數(shù)詳見教材第82頁。
范圍for語句
關(guān)于范圍for循環(huán)語句:
范圍for語句的語法形式是:
其中expression部分是一個對象,用于表示一個序列,declaration部分負責定義一個變量,該變量將被用于訪問序列中的基礎(chǔ)元素。每次迭代,declaration部分的變量會被初始化為expression部分的下一個元素值。使用該語句可以很方便地處理string中的每一個元素。例如:
for (auto &c : str) //str是一個字符串c = ‘X’; //每行輸出一個str中的一個字符上述代碼的作用是遍歷str中的每一個元素,使每一個元素都變?yōu)閄。關(guān)鍵字auto讓編譯器決定變量c的類型,這里c的類型是char。其中&符號表示c是引用,目前我學到的是只有通過引用或解引用指針的形式才能改變數(shù)組或string中的元素。
string定義的迭代器及其他
string::iterator與string::const_iterator是string定義的迭代器類型,分別為非常量迭代器和常量迭代器,其作用與指針有相似之處,都可以進行遞增、遞減操作,都可以通過解引用獲得string中的元素。非常量迭代器允許改變其所指向的元素,常量迭代器不允許。
string成員函數(shù)begin、end與size的用途和原理,與實驗一中vector的同名成員函數(shù)是類似的,其中size返回的類型是string::size_type。
關(guān)于下標操作
string類型支持用下標表示法操作其中的元素,下標返回的是一個引用,故可以通過下標改變string其中的元素。
示例代碼
練習3.6
#include <iostream> //聲明使用命名空間std中的名字 using namespace std;#include <string>int main() {string line; //聲明變量line,用于臨時存儲字符串getline(cin, line); //讀入字符串//聲明引用ch,這樣才能改變line中的字符//使用范圍for遍歷line中的每個字符for (auto &ch : line) ch = 'X'; //將每個字符改變?yōu)?#39;X'cout << line << endl;system("pause");return 0; }cctype.cpp
#include <string> //聲明使用string using std::string;//包含cctype頭文件,以使用其中多種判斷字符特性的函數(shù) #include <cctype> //聲明使用一系列用得到的函數(shù)名字 using std::isupper; using std::toupper; using std::islower; using std::tolower; using std::isalpha; using std::isspace;#include <iostream> //聲明使用cout、endl using std::cout; using std::endl;int main() {string s("Hello World!!!");//punct_cnt的類型是string::size_type,與string成員函數(shù)size的返回值相同//string::size_type是string類定義的配套的類型,能夠放下任何string對象的大小string::size_type punct_cnt = 0; //統(tǒng)計s中標點符號的個數(shù)//使用for循環(huán)對s中的每個元素(字符)進行遍歷/*判斷條件:c的值不等于s的大小,說明還未遍歷完整個string同時c的值也可以作為s的下標用于隨機訪問*/for (string::size_type c = 0; c != s.size(); ++c)if (ispunct(s[c])) //如果s中下標為c的字符是標點符號++punct_cnt; //則增加標點符號的統(tǒng)計數(shù)cout << punct_cnt << " punctuation characters in " << s << endl;//備份sstring orig = s;//使s中每個字符都變成大寫的形式for (string::size_type c = 0; c != s.size(); ++c)s[c] = toupper(s[c]); //s[c]返回一個引用以改變s中的元素cout << s << endl;//下面是使s中的第一個單詞變成大寫的形式s = orig; //還原sstring::size_type index = 0;//執(zhí)行循環(huán)直到s結(jié)束或遇到第一個空白(即讀完第一個單詞)while (index != s.size() && !isspace(s[index])) {s[index] = toupper(s[index]); //使字符變成大寫的形式++index; //移動到下一個字符,進行下一輪循環(huán)}cout << s << endl;system("pause");return 0; }punctRemove.cpp
#include <iostream> #include <string> #include <cctype> //聲明使用命名空間std中的名字using namespace std;int main() {//聲明string類型s、result//s用于存儲讀入的字符串//esult用于存儲除去s中標點符號后的結(jié)果string s,result;cout << "Please enter a string,it's best to include some punctuation marks. " << endl;getline(cin,s); //讀入字符串//decltype類型指示符用于選擇并返回其操作表達式的數(shù)據(jù)類型//在此過程中,編譯器分析表達式并得到它的類型,卻不實際計算表達式的值//一般不知道使用何種類型的變量時,可以使用此指示符自動計算數(shù)據(jù)類型//此處i的類型是string成員函數(shù)size的返回類型:string::size_typefor(decltype(s.size()) i = 0 ; i < s.size(); i++){if(!ispunct(s[i])) //如果s[i]不是標點符號result += s[i]; //則把該字符添加到result中}cout << result << endl; //輸出結(jié)果system("pause");return 0; }char_repl.cpp
#include <string> //聲明使用string using std::string;//包含cctype頭文件,以使用其中多種判斷字符特性的函數(shù) #include <cctype> //聲明使用一系列用得到的函數(shù)名字 using std::isupper; using std::toupper; using std::islower; using std::tolower; using std::isalpha; using std::isspace;#include <iostream> //聲明使用cout、endl using std::cout; using std::endl;int main() {string str("some string"), orig = str; //orig是str的備份if (!str.empty()) //確保str有存儲輸入cout << str[0] << endl; //輸出str的第一個字符if (!str.empty()) //確保str的第一個字符不為空str[0] = toupper(str[0]); //使第一個字符變成大寫形式cout << str << endl; //輸出strstr = orig; //還原str//用迭代器來實現(xiàn)上述功能if (str.begin() != str.end()) { //確保str有存儲輸入的另一種形式string::iterator it = str.begin(); //string的迭代器it指向str的首元素*it = toupper(*it); //通過解引用迭代器實現(xiàn)第一個字符大寫化}cout << str << endl; //輸出strstr = orig; //還原str//大寫化str中第一個單詞的四種方法://1.使用for循環(huán)并通過下標的形式//index的類型是string::size_type,與string成員函數(shù)size的返回值相同,作為下標for (string::size_type index = 0; //判斷條件:當index不等于str的大小,且以index為下標的元素不是空白時執(zhí)行語句index != str.size() && !isspace(str[index]); ++index) str[index] = toupper(str[index]); //大寫化當前字符cout << str << endl; //輸出strstr = orig; //還原str//2.使用for循環(huán)并使用迭代器取代使用下標//string的迭代器it指向str的首元素//判斷條件:當it不指向str的尾后元素,且it指向的元素不是空白時執(zhí)行語句for (string::iterator it = str.begin(); it != str.end() && !isspace(*it); ++it)*it = toupper(*it); //通過解引用迭代器實現(xiàn)字符大寫化cout << str << endl; //輸出strstr = orig; //還原str//3.使用while循環(huán)并通過下標的形式//index的類型是string::size_type,與string成員函數(shù)size的返回值相同,作為下標string::size_type index = 0;//判斷條件:當index不等于str的大小,且以index為下標的元素不是空白時執(zhí)行語句while (index != str.size() && !isspace(str[index])) {str[index] = toupper(str[index]); //大寫化當前字符++index; //增加下標值以獲得下一個字符}cout << str << endl; //輸出str//4.使用while循環(huán)并使用迭代器取代使用下標//string的迭代器beg指向str的首元素string::iterator beg = str.begin();//判斷條件:當beg不指向str的尾后元素,且beg指向的元素不是空白時執(zhí)行語句while (beg != str.end() && !isspace(*beg)) {*beg = toupper(*beg); //通過解引用迭代器實現(xiàn)字符大寫化++beg; //使迭代器指向下一個元素}cout << str << endl; //輸出strstr = orig; //還原str//使用for循環(huán)操作str中的每個字符//首先是每行輸出str中的一個字符,使用的是常量迭代器,不會改變其所指向的元素for (string::const_iterator c = str.begin(); c != str.end(); ++c)cout << *c << endl; //通過解引用每行輸出一個字符//改變str中的每個字符,使用的是可以改變其指向元素的迭代器for (string::iterator c = str.begin(); c != str.end(); ++c)*c = '*'; //通過解引用迭代器將每個字符變?yōu)?#39;*'cout << str << endl; //輸出strstr = orig; //還原str//用下標實現(xiàn)上述功能//首先是每行輸出str中的一個字符for (string::size_type ix = 0; ix != str.size(); ++ix)cout << str[ix] << endl; //使用下標每行輸出一個字符//改變str中的每個字符for (string::size_type ix = 0; ix != str.size(); ++ix)str[ix] = '*'; //通過下標將每個字符變?yōu)?#39;*'cout << str << endl; //輸出strstr = orig; //還原str//使用for循環(huán)和迭代器實現(xiàn)上述功能//首先是每行輸出str中的一個字符,使用的是常量迭代器,不會改變其所指向的元素for (string::const_iterator beg = str.begin(); beg != str.end(); ++beg)cout << *beg << endl; //通過解引用每行輸出一個字符//改變str中的每個字符for (string::iterator beg = str.begin(); beg != str.end(); ++beg)*beg = '*'; //通過解引用迭代器將每個字符變?yōu)?#39;*'cout << str << endl; //輸出strsystem("pause");return 0; }總結(jié)
以上是生活随笔為你收集整理的标准库类型string的基本功能和使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多个Activity与Fragment之
- 下一篇: Kali Linux ver2020.4