国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ STL 學習筆記 string

2019-11-06 08:01:54
字體:
來源:轉載
供稿:網友

string 字符串 定義和初始化

string s1;string s2(str); //使用構造函數初始化string s3(n,'c'); //s3是有n個連續的c字符組成的字符串

string對象上的操作

getline(is,s) //從輸入流中讀取一行字符串賦給ss.empty()s.append(str) //在s末尾加一個字符串s.size()s[i]s1==s2 //如果s1和s2的所含的字符完全一樣,則相等s.length();s.erase(string::iterator it) //刪除it處的字符s.erase(str.begin(),str.end()) //刪除該范圍內的字符reverse(str.begin(),str.end()) //將該范圍內的字符反轉transform(str.begin(),str.end(),str.begin(),toupper)//將begin到end出的字符轉換成大寫字母,并從beign出寫入,小寫是tolower

學習代碼

c和c++風格的字符串

#include <iostream>#include <string>#PRagma warning(disable:4996)//strcpy不安全被禁用,要想使用采用以下辦法和加上 上面的注釋信息/*其實在下面的輸出錯誤信息中有解決方法,“To disable deprecation , use _CRT_SECURE_NO_WARNINGS”,意思是我們可以不進行兼容性檢查,我們可以在項目-屬性-配置屬性-c/c++-預處理器-預處理定義里邊加上一句:_CRT_SECURE_NO_WARNINGS */int main(){ using namespace std; char pszName[20] = "張飛"; //c風格的字符串 cout << pszName << endl; char *pszName2 = "張飛"; cout << pszName2 << endl; string strName2("劉備"); cout << strName2 << endl; string strName = "張飛"; //運算符重載 cout << strName << endl; const char* pszConstString = "Hello String!"; cout << pszConstString << endl; string strFormatConst(pszConstString); //使用string的構造函數 cout << strFormatConst << endl; string strCopy(pszConstString, 5); //復制前5個字符 cout << strCopy << endl; //c風格字符串的復制 const char* constStr = "Hello C!"; char *pszCopy = new char[strlen(constStr) + 1]; strcpy(pszCopy, constStr); cout << pszCopy << endl; delete[] pszCopy; getchar(); return 0;}

訪問字符串中的元素以及字符串的連接

#include <iostream>#include <string>using namespace std;int main(){ string strStlString("hello Word"); cout << "使用傳統的方法顯示字符串中的每一個字符" << endl; for (size_t i = 0;i< strStlString.length(); ++i) { cout << strStlString[i] << endl; } cout << endl; cout << "使用STL里的迭代器操作字符串中的字符" << endl; string str1 = "ni hao"; string str2 = "this is fun!"; string::iterator i; for (i = strStlString.begin();i!=strStlString.end();++i) { cout << *i << endl; } //字符串的連接 str1 += str2; cout << str1 << endl; str1.append(str2); cout << str1 << endl; getchar(); return 0;}

字符串的查找

#include <iostream>#include <string>using namespace std;int main(){ string strSample = "Hello World! Today is a good day"; //字符串的查找 size_t positon = strSample.find("ll", 0); if (positon != string::npos) { cout << "找到了!" << endl; cout << positon << endl; } else { cout << "沒有找到哦" << endl; } //查找多個相同的子串 size_t position1 = strSample.find("day", 0); while (position1 != string::npos) { cout << "在下標" << position1 << "出找到子串day" << endl; size_t position2 = position1 + 1; position1 = strSample.find("day", position2); } //查找多個相同的字符 size_t position3 = strSample.find('o', 0); while (position3 != string::npos) { cout << "在下標" << position3 << "處找到o" << endl; size_t position4 = position3 + 1; position3 = strSample.find('o', position4); } getchar(); return 0;}

字符串中元素的刪除及反轉

#include <iostream>#include <string>#include <algorithm>int main(){ using namespace std; string strSample = "Hello String ! Wake up to a beautiful day"; cout << strSample << endl; strSample.erase(13, 28); cout << strSample << endl; //find算法,迭代器 string::iterator iChars = find(strSample.begin(), strSample.end(),'S'); if (iChars != strSample.end()) { strSample.erase(iChars); } cout << strSample << endl; strSample.erase(strSample.begin(), strSample.end()); if (strSample.length()==0) cout << "this is a empty string" << endl; //字符串反轉算法 string strSample2 = "Hello String! We will reverse you!"; cout << strSample2 << endl; reverse(strSample2.begin(), strSample2.end()); cout << strSample2 << endl; // cout << "請輸入一行字符串" << endl; string strInput; getline(cin, strInput); transform(strInput.begin(), strInput.end(),strInput.begin(),toupper);//從begin到end轉換,轉換后又存到以beign開始的地方 cout << strInput << endl; transform(strInput.begin(), strInput.end(), strInput.begin(), tolower); cout << strInput << endl; getchar(); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 上饶县| 连城县| 云安县| 抚顺县| 泾川县| 萨迦县| 龙泉市| 云林县| 武宁县| 黔西| 镇平县| 邯郸县| 云南省| 金堂县| 柯坪县| 葵青区| 保定市| 滨州市| 饶河县| 洛扎县| 富蕴县| 同仁县| 册亨县| 武义县| 北碚区| 连山| 内江市| 海口市| 河西区| 灵丘县| 化德县| 林芝县| 金华市| 来凤县| 喀什市| 云和县| 阿荣旗| 德安县| 万全县| 凤山市| 万盛区|