C++ STL string 簡單使用

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <string>
#include <stdexcept>
/*
string 構造函數
string();//創建一個空的字符串 例如: string str;
string(const string& str);//使用一個string對象初始化另一個string對象
string(const char* s);//使用字符串s初始化
string(int n, char c);//使用n個字符c初始化3.1.2.2 string基本賦值操作
string& operator=(const char* s);//char*類型字符串 賦值給當前的字符串
string& operator=(const string &s);//把字符串s賦給當前的字符串
string& operator=(char c);//字符賦值給當前的字符串
string& assign(const char *s);//把字符串s賦給當前的字符串
string& assign(const char *s, int n);//把字符串s的前n個字符賦給當前的字符串
string& assign(const string &s);//把字符串s賦給當前字符串
string& assign(int n, char c);//用n個字符c賦給當前字符串
string& assign(const string &s, int start, int n);//將s從start開始n個字符賦值給字符串*/void test01()
{string str; //默認構造string str2(str); //拷貝構造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個字符賦給當前的字符串str3.assign("abcdef", 4);cout << str3 << endl;//string& assign(const string &s, int start, int n);//將s從start開始n個字符賦值給字符串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區別?[]訪問越界  直接掛掉 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連接到當前字符串結尾
string& append(const char *s, int n);//把字符串s的前n個字符連接到當前字符串結尾
string& append(const string &s);//同operator+=()
string& append(const string &s, int pos, int n);//把字符串s中從pos開始的n個字符連接到當前字符串結尾
string& append(int n, char c);//在當前字符串結尾添加n個字符c3.1.2.5 string查找和替換
int find(const string& str, int pos = 0) const; //查找str第一次出現位置,從pos開始查找
int find(const char* s, int pos = 0) const;  //查找s第一次出現位置,從pos開始查找
int find(const char* s, int pos, int n) const;  //從pos位置查找s的前n個字符第一次位置
int find(const char c, int pos = 0) const;  //查找字符c第一次出現位置
int rfind(const string& str, int pos = npos) const;//查找str最后一次位置,從pos開始查找
int rfind(const char* s, int pos = npos) const;//查找s最后一次出現位置,從pos開始查找
int rfind(const char* s, int pos, int n) const;//從pos查找s的前n個字符最后一次位置
int rfind(const char c, int pos = 0) const; //查找字符c最后一次出現位置
string& replace(int pos, int n, const string& str); //替換從pos開始n個字符為字符串str
string& replace(int pos, int n, const char* s); //替換從pos開始的n個字符為字符串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 結果一樣,內部查找順序相反cout << "pos2 = " << pos2 << endl; // 4 2 //替換string s3 = "hello"; //替換從pos開始n個字符為字符串strs3.replace(1, 3, "1111");cout << s3 << endl; // h1111o}/*
string比較操作
/*
compare函數在>時返回 1,<時返回 -1,==時返回 0。
比較區分大小寫,比較時參考字典順序,排越前面的越小。
大寫的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個字符組成的字符串*/void test05()
{string s1 = "abcde";string s2 = s1.substr(1, 3);cout << "s2 = " << s2 << endl;//需求  查找一個右鍵的 用戶名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個字符c
string& erase(int pos, int n = npos);//刪除從Pos開始的n個字符*/void test06()
{string s1 = "hello";s1.insert(1, "111");cout << s1 << endl; //h111ello//刪除 111s1.erase(1, 3);cout << s1 << endl;}/*
string和c-style字符串轉換
*/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 * 隱式類型轉換為 string//const char * -> string string s2(p);//func2(s2); //string 不能隱式類型轉換為 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;}/*
寫一個函數,函數內部將string字符串中的所有小寫字母都變為大寫字母。
*/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;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/382581.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/382581.shtml
英文地址,請注明出處:http://en.pswp.cn/news/382581.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

KMP算法的舉例加圖解

文章出處&#xff1a;阮一峰&#xff0c;進行重新排版整理 舉例來說&#xff0c;有一個字符串"BBC ABCDAB ABCDABCDABDE"&#xff0c;我想知道&#xff0c;里面是否包含另一個字符串"ABCDABD"&#xff1f; 首先&#xff0c;字符串"BBC ABCDAB ABCDAB…

C++ STL 容器之 deque簡單使用

#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #include <deque> #include <algorithm> /* deque構造函數 deque<T> deqT;//默認構造形式 deque(beg, end);//構造函數將[beg, end)區間中的元素拷貝給本身。 deque(n, elem…

進程間通訊的四種方式

文章目錄共享內存信號管道消息隊列通信方法 無法介于內核態與用戶態的原因 管道&#xff08;不包括命名管道&#xff09; 局限于父子進程間的通信。 消息隊列 在硬、軟中斷中無法無阻塞地接收數據。 信號量 無法介于內核態和用戶態使用。 共享內存 需要信號量輔助&#xff0c;而…

TCP/IP四層模型

文章目錄TCP/IP協議族體系結構以及主要協議數據鏈路層網絡層傳輸層應用層TCP/IP協議族體系結構以及主要協議 TCP/IP協議族是一個四層協議系統&#xff0c;自底而上分別是數據鏈路層、網絡層、傳輸層和應用層。每一層完成不同 的功能&#xff0c;且通過若干協議來實現&#xff…

C++ STL 容器之stack簡單使用

#include <iostream> #include <stack> #include <string> using namespace std; void test1() {stack<int> s;s.push(10);s.push(20);s.push(30);while (s.size()) {cout << "stack top is" << s.top() << endl; // 棧頂…

LRU緩存算法緩存設計和實現

什么是緩存&#xff1f; 舉個例子&#xff0c;去圖書館查資料&#xff0c;一般情況下我們會集中把我們有可能查閱的幾本書從書架取下來&#xff0c;放在我們的桌面上&#xff0c;以便交叉查閱&#xff0c;從而避免頻繁的從座位上跑到書架旁去取書。在這個例子里&#xff0c;書…

C++ STL 容器之queue

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<queue> using namespace std;/*Queue所有元素的進出都必須符合”先進先出”的條件&#xff0c; 只有queue的頂端元素&#xff0c; 才有機會被外界取用。 Queue不提供遍歷功能&#xff0c; 也不提供迭…

牛客網選擇題之并發

在分時操作系統中&#xff0c;進程調度采用&#xff08;&#xff09;算法 時間片輪轉某系統中有 3 個并發進程&#xff0c;都需要同類資源 4 個&#xff0c;試問該系統不會發生死鎖的最少資源數是&#xff1a;9 有n個進程&#xff0c;共享的同類資源數為m&#xff0c;則避免死鎖…

牛客網選擇題之linux

1.在RHEL5系統中&#xff0c;小王希望將他執行的ls命令的輸出結果保存在當前目錄下文件output.ls中&#xff0c;以供日后進行分析和使用&#xff0c;但要求不覆蓋原文件的內容&#xff0c;他應該使用的命令是&#xff08; &#xff09; ls>>output.ls > …

C++ STL容器之 list 初步

#include <iostream> #include<algorithm> #include <string> #include <list> using namespace std;//3.6.4.1 list構造函數 //list<T> lstT;//list采用采用模板類實現,對象的默認構造形式&#xff1a; //list(beg, end);//構造函數將[beg, end…

C++ STL容器值set

/* 3.7.2 set常用API 3.7.2.1 set構造函數 set<T> st;//set默認構造函數&#xff1a; mulitset<T> mst; //multiset默認構造函數: set(const set& st);//拷貝構造函數 3.7.2.2 set賦值操作 set & operator(const set & st);//重載等號操作符 swap(st)…

C++ STL容器之map 簡單使用

3.8.2.1 map構造函數 map<T1, T2> mapTT;//map默認構造函數: map(const map &mp);//拷貝構造函數3.8.2.2 map賦值操作 map& operator(const map &mp);//重載等號操作符 swap(mp);//交換兩個集合容器3.8.2.3 map大小操作 size();//返回容器中元素的數目 empty…

Manacher算法圖解

看了好久的Manacher算法&#xff0c;覺得還是要自己畫一遍&#xff0c;自己把代碼寫一遍才能理解 下面分享一下&#xff0c;如果有錯&#xff0c;希望指正 簡陋版本的&#xff0c;但是他基本只是做到了求取最長回文字符串&#xff0c;嚴格來說它并不是Manacher’s Algorithm-…

Flink 客戶端操作命令及可視化工具

Flink提供了豐富的客戶端操作來提交任務和與任務進行交互。下面主要從Flink命令行、Scala Shell、SQL Client、Restful API和 Web五個方面進行整理。 在Flink安裝目錄的bin目錄下可以看到flink&#xff0c;start-scala-shell.sh和sql-client.sh等文件&#xff0c;這些都是客戶…

ySQL挑戰搭建一個簡易的成績管理系統的數據庫

文章為自己搜索網上資源&#xff0c;再在這里進行整理&#xff0c;所以標注為轉載 [實驗步驟](https://www.shiyanlou.com/courses/reports/1347700) 總結做實驗注意事項&#xff1a; 1.添加主鍵 2.主鍵和外鍵的關系 3.注意自增的書寫添加 mysql 如何修改、添加、刪除表主鍵…

網絡之DNS協議圖解

DNS是計算機域名系統 (Domain Name System) 域名系統采用類似目錄樹的等級結構。 域名服務器是指保存有該網絡中所有主機的域名和對應IP地址&#xff0c;并具有將域名轉換為IP地址功能的服務器。 域名服務器為客戶機/服務器模式中的服務器方&#xff0c;它主要有兩種形式&am…

C++ 謂詞,

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <vector> #include <algorithm> using namespace std;class GreaterThen20 { public:bool operator()(int val){return val > 20;} };//一元謂詞 void test01() {vector<int>v;v.push…

網絡之ARP

地址解析協議&#xff0c;即ARP&#xff08;Address Resolution Protocol&#xff09;&#xff0c;是根據IP地址獲取物理地址的一個TCP/IP協議。 主機發送信息時將包含目標IP地址的ARP請求廣播到網絡上的所有主機&#xff0c;并接收返回消息&#xff0c;以此確定目標的物理地址…

C++ 內建函數對象

STL內建了一些函數對象。分為:算數類函數對象,關系運算類函數對象&#xff0c;邏輯運算類仿函數。這些仿函數所產生的對象&#xff0c;用法和一般函數完全相同&#xff0c;當然我們還可以產生無名的臨時對象來履行函數功能。使用內建函數對象&#xff0c;需要引入頭文件 functi…

網絡之ICMP協議

ICMP 主要功能&#xff1a; 確認IP包是否成功送達目標地址通知在發送過程當中IP包被廢棄的具體原因改善網絡設置等 在IP通信中如果某個IP包因為某種原因未到達目標地址&#xff0c;那么這個原因由ICMP通知。 過程&#xff08;圖解TCP/IP&#xff09; ICMP類型 常見的&am…