日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ STL string 简单使用

發(fā)布時(shí)間:2023/11/30 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ STL string 简单使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #include <string> #include <stdexcept> /* string 構(gòu)造函數(shù) string();//創(chuàng)建一個(gè)空的字符串 例如: string str; string(const string& str);//使用一個(gè)string對象初始化另一個(gè)string對象 string(const char* s);//使用字符串s初始化 string(int n, char c);//使用n個(gè)字符c初始化3.1.2.2 string基本賦值操作 string& operator=(const char* s);//char*類型字符串 賦值給當(dāng)前的字符串 string& operator=(const string &s);//把字符串s賦給當(dāng)前的字符串 string& operator=(char c);//字符賦值給當(dāng)前的字符串 string& assign(const char *s);//把字符串s賦給當(dāng)前的字符串 string& assign(const char *s, int n);//把字符串s的前n個(gè)字符賦給當(dāng)前的字符串 string& assign(const string &s);//把字符串s賦給當(dāng)前字符串 string& assign(int n, char c);//用n個(gè)字符c賦給當(dāng)前字符串 string& assign(const string &s, int start, int n);//將s從start開始n個(gè)字符賦值給字符串*/void test01() {string str; //默認(rèn)構(gòu)造string str2(str); //拷貝構(gòu)造string str3 = str;string str4 = "abcd";string str5(10, 'a');cout << str4 << endl;cout << str5 << endl;//基本賦值str = "hello";str2 = str4;//string& assign(const char *s, int n);//把字符串s的前n個(gè)字符賦給當(dāng)前的字符串str3.assign("abcdef", 4);cout << str3 << endl;//string& assign(const string &s, int start, int n);//將s從start開始n個(gè)字符賦值給字符串string str6;str6.assign(str, 1, 3); //ell ? hel 從0索引cout << str6 << endl; }/* string存取字符操作 char& operator[](int n);//通過[]方式取字符 char& at(int n);//通過at方法獲取字符*/ void test02() {string s = "hello world";for (int i = 0; i < s.size();i++){//cout << s[i] << endl;cout << s.at(i) << endl;}//[] 和at區(qū)別?[]訪問越界 直接掛掉 at會拋出異常try{//cout << s[100] << endl;cout << s.at(100) << endl;}catch (out_of_range & e){cout << e.what() << endl;}catch (...){cout << "越界異常" << endl;} }/* string拼接操作 string& operator+=(const string& str);//重載+=操作符 string& operator+=(const char* str);//重載+=操作符 string& operator+=(const char c);//重載+=操作符 string& append(const char *s);//把字符串s連接到當(dāng)前字符串結(jié)尾 string& append(const char *s, int n);//把字符串s的前n個(gè)字符連接到當(dāng)前字符串結(jié)尾 string& append(const string &s);//同operator+=() string& append(const string &s, int pos, int n);//把字符串s中從pos開始的n個(gè)字符連接到當(dāng)前字符串結(jié)尾 string& append(int n, char c);//在當(dāng)前字符串結(jié)尾添加n個(gè)字符c3.1.2.5 string查找和替換 int find(const string& str, int pos = 0) const; //查找str第一次出現(xiàn)位置,從pos開始查找 int find(const char* s, int pos = 0) const; //查找s第一次出現(xiàn)位置,從pos開始查找 int find(const char* s, int pos, int n) const; //從pos位置查找s的前n個(gè)字符第一次位置 int find(const char c, int pos = 0) const; //查找字符c第一次出現(xiàn)位置 int rfind(const string& str, int pos = npos) const;//查找str最后一次位置,從pos開始查找 int rfind(const char* s, int pos = npos) const;//查找s最后一次出現(xiàn)位置,從pos開始查找 int rfind(const char* s, int pos, int n) const;//從pos查找s的前n個(gè)字符最后一次位置 int rfind(const char c, int pos = 0) const; //查找字符c最后一次出現(xiàn)位置 string& replace(int pos, int n, const string& str); //替換從pos開始n個(gè)字符為字符串str string& replace(int pos, int n, const char* s); //替換從pos開始的n個(gè)字符為字符串s*/void test03() {//拼接string s1 = "我";string s2 = "愛北京";s1 += s2;cout << s1 << endl;s1.append("天安門");cout << s1 << endl;//find查找string s = "abcdefg";int pos = s.find("bcf"); //找不到返回是 -1cout << "pos = " << pos << endl;int pos2 = s.rfind("bc"); //rfind 和find 結(jié)果一樣,內(nèi)部查找順序相反cout << "pos2 = " << pos2 << endl; // 4 2 //替換string s3 = "hello"; //替換從pos開始n個(gè)字符為字符串strs3.replace(1, 3, "1111");cout << s3 << endl; // h1111o}/* string比較操作 /* compare函數(shù)在>時(shí)返回 1,<時(shí)返回 -1,==時(shí)返回 0。 比較區(qū)分大小寫,比較時(shí)參考字典順序,排越前面的越小。 大寫的A比小寫的a小。int compare(const string &s) const;//與字符串s比較 int compare(const char *s) const;//與字符串s比較 */void test04() {string s1 = "abc";string s2 = "abcd";if (s1.compare(s2) == 0){cout << "s1 等于 s2" << endl;}else if (s1.compare(s2) == 1){cout << "s1 大于 s2" << endl;}else{cout << "s1 小于 s2" << endl;}}/* string子串 string substr(int pos = 0, int n = npos) const;//返回由pos開始的n個(gè)字符組成的字符串*/void test05() {string s1 = "abcde";string s2 = s1.substr(1, 3);cout << "s2 = " << s2 << endl;//需求 查找一個(gè)右鍵的 用戶名string email = "zhangtao@sina.com";int pos = email.find("@");//8 cout << "pos " << pos << endl;string usrName = email.substr(0, pos);cout << "用戶名為:" << usrName << endl; }/* string插入和刪除操作 string& insert(int pos, const char* s); //插入字符串 string& insert(int pos, const string& str); //插入字符串 string& insert(int pos, int n, char c);//在指定位置插入n個(gè)字符c string& erase(int pos, int n = npos);//刪除從Pos開始的n個(gè)字符*/void test06() {string s1 = "hello";s1.insert(1, "111");cout << s1 << endl; //h111ello//刪除 111s1.erase(1, 3);cout << s1 << endl;}/* string和c-style字符串轉(zhuǎn)換 */void func(string s) {cout << s << endl; }void func2(const char * s) {cout << s << endl; }void test07() {string s = "abc";//string -> const char *const char * p = s.c_str();func(p); //const char * 隱式類型轉(zhuǎn)換為 string//const char * -> string string s2(p);//func2(s2); //string 不能隱式類型轉(zhuǎn)換為 char * }void test08() {string s = "abcdefg";char& a = s[2];char& b = s[3];a = '1';b = '2';cout << s << endl;cout << (int*)s.c_str() << endl;s = "pppppppppppppp";//a = '1';//b = '2';cout << s << endl;cout << (int*)s.c_str() << endl;}/* 寫一個(gè)函數(shù),函數(shù)內(nèi)部將string字符串中的所有小寫字母都變?yōu)榇髮懽帜浮?*/void test09() {string s = "abCdEfg";for (int i = 0; i < s.size();i++){//s[i] = toupper(s[i]);//全變小寫s[i] = tolower(s[i]);}cout << s << endl; }int main(){// test01();//test02();//test03();//test04();//test05();//test06();// test07();//test08();test09();system("pause");return EXIT_SUCCESS; }

總結(jié)

以上是生活随笔為你收集整理的C++ STL string 简单使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。