1樓:小王同學在積累
一、筆記
二、筆記目錄
① string是C++風格的字串,而string本質上是乙個類。
② string 和 char * 區別:
1. char * 是乙個指標
2. string 是乙個類,類內部封裝了 char *,管理這個字串是乙個char型容器。
③ string特點:
① string建構函式原型:
1. string建立乙個空的字串例如:string str;
2. string(const char * s); // 使用字串s初始化
3. string(const string & str); // 使用乙個string物件初始化另乙個string物件
4. string(int n,char c); //使用n個字元c初始化
② string的多種構造方式沒有可比性,靈活使用即可。
#include
using namespace std;
#include
//string的建構函式
/*1. string建立乙個空的字串例如:string str;
2. string(const char* s); // 使用字串s初始化
3. string(const string & str); // 使用乙個string物件初始化另乙個string物件
4. string(int n, char c); //使用n個字元c初始化
*/void test01()
int main()
執行結果:
s1 =
s2 = hello world
s3 = hello world
s4 = aaaaaaaaaa
① 給string字串進行賦值。
② 賦值的函式原型:
1. string& operator=(const char* s); //char * 型別字串賦值給當前的字串
2. string& operator=(const strinng &s); //把字串s賦給當前的字串
3. string& operator=(char c); //字元賦值給當前的字串
4. string& assign(const char *s); //把字串s賦給當前的字串
5. string& assign(const char *s,int n); //把字串s的前n個字元賦給當前的字串
6. string& assign(const string &s); //把字串s賦給當前字串
7. string& assign(int n, char c); //用n個字元c賦給當前字串
③ string的賦值方式很多,operator=這種方式是比較常用的。
#include
using namespace std;
#include
//string賦值操作
/*1. string& operator=(const char* s); //char * 型別字串賦值給當前的字串
2. string& operator=(const string &s); //把字串s賦給當前的字串
3. string& operator=(char c); //字元賦值給當前的字串
4. string& assign(const char *s); //把字串s賦給當前的字串
5. string& assign(const char *s,int n); //把字串s的前n個字元賦給當前的字串
6. string& assign(const string &s); //把字串s賦給當前字串
7. string& assign(int n, char c); //用n個字元c賦給當前字串
*/void test01()
int main()
執行結果:
str1 = hello world
str2 = hello world
str3 = a
str4 = hello C++
str5 = hello
str6 = hello
str7 = wwwwwwwwww
① 實現在字串末尾拼接字串。
② 函式原型:
#include
using namespace std;
#include
//string賦值操作
/*1. string& operator+=(const char* str); //過載+=操作符
2. string& operator+=(const char c); //過載+=操作符
3. string& operator+=(const string& str); //過載+=操作符
*/void test01()
int main()
執行結果:
str1 = 我愛玩遊戲
str1 = 我愛玩遊戲:
str1 = 我愛玩遊戲: LOL DNF
str3 = I love
str3 = I love game
str3 = I love game LOL DNF
str3 = I love game LOL DNF LOL
① 查詢:查詢指定字串是否存在。
② 替換:在指定的位置替換字串。
③ 函式原型:
//查詢str第一次出現位置,從pos開始查詢
1. int find(const string& str, int pos = 0) const;
// 查詢s第一次出現位置,從pos開始查詢
2. int find(const char* s, int pos = 0) const;
//從pos位置查詢s的前n個字元第一次位置
3. int find(const char* s, int pos, int n) const;
//查詢字元c第一次出現位置
4. int find(const char c, int pos = 0) const;
//查詢str最後一次位置,從pos開始查詢
5. int rfind(const string& str, int pos = npos) const;
//查詢s最後一次出現位置,從pos開始查詢
6. int rfind(const char* s, int pos = npos) const;
//從pos查詢s的前n個字元最後一次位置
7. int rfind(const char* s, int pos, int n) const;
//查詢字元c最後一次出現位置
8. int rfind(const char c, int pos = 0) const;
//替換從pos開始n個字元為字串str
9. string& replace(int pos, int n, const string& str) const;
//替換從pos開始的n個字元為s
10. string& replace(int pos, int n, const string* s) const;
④ find查詢是從左往右,rfind從右往左。
⑤ find找到字串後返回查詢的第乙個字元位置,找不到返回-1。
⑥ replace在替換時,要知道從哪個位置起,多少個字元,替換成什麼樣的字串。
#include
using namespace std;
#include
//string查詢和替換
/*//查詢str第一次出現位置,從pos開始查詢
1. int find(const string& str, int pos = 0) const;
// 查詢s第一次出現位置,從pos開始查詢
2. int find(const char* s, int pos = 0) const;
//從pos位置查詢s的前n個字元第一次位置
3. int find(const char* s, int pos, int n) const;
//查詢字元c第一次出現位置
4. int find(const char c, int pos = 0) const;
//查詢str最後一次位置,從pos開始查詢
5. int rfind(const string& str, int pos = npos) const;
//查詢s最後一次出現位置,從pos開始查詢
6. int rfind(const char* s, int pos = npos) const;
//從pos查詢s的前n個字元最後一次位置
7. int rfind(const char* s, int pos, int n) const;
//查詢字元c最後一次出現位置
8. int rfind(const char c, int pos = 0) const;
//替換從pos開始n個字元為字串str
9. string& replace(int pos, int n, const string& str) const;
//替換從pos開始的n個字元為s
10. string& replace(int pos, int n, const string* s) const;
*///1、查詢
void test01()
else
//rfind
pos = str1.rfind("de"); //rfind是從右往左查詢,find是從左往右查詢
cout << "pos=" << pos << endl;
}void test02()
int main()
執行結果:
找到字串 pos = 3
pos=7
str1= a1111efg
① 功能描述:字串之間比較。
② 比較方式:字串比較是按字元的ASCII碼進行對比。
1. = 返回 0
2. > 返回 1
3. < 返回 -1
③ 函式原型:
1. int compare(const string & s) const; //與字串s比較
2. int compare(const char * s) const; //與字串s比較
#include
using namespace std;
#include
//字串比較
//1、查詢
void test01()
else if (str1.compare(str2) > 0)
else
執行結果:
str1 等於 str2
① string中單個字元訪問方式有兩種:
1. char& operator(int n); //通過方式取字元
2. char& at(int n); //通過at方式取字元
#include
using namespace std;
#include
//string 字元訪問
void test01()
cout << endl; //換行符
//2、通過at方式訪問單個字元
for (int i = 0; i < str.size(); i++)
cout << endl;
//修改單個字元
str[0] = 'x';
cout << "str= " << str << endl;
str.at(1) = 'x';
cout << "str= " << str << endl;
}int main()
執行結果:
str= hello
h e l l o
h e l l o
str= xello
str= xxllo
② 函式原型:
#include
using namespace std;
#include
void test01()
int main()
執行結果:
str = h111ello
str = hello
① 功能描述:從字串中獲取想要的子串。
② 函式原型:
1. string substr(int pos = 0, int n = npos) const; //返回由pos開始的n個字組成的字串。
③ 靈活的運用求子串功能,可以在實際開發中獲取有效的資訊。
#include
using namespace std;
#include
//string 求子串
void test01()
//實用操作
void test02()
int main()
執行結果:
subStr = bcd
8zhangsan
01 筆記--Python [ 整理完 ]
02 筆記--機器視覺 [ 整理完 ]
03 筆記--C++ [整理完]
04 筆記--爬蟲 [整理ing]
01 如何最簡單、通俗地理解C++的注釋?
02 如何最簡單、通俗地理解C++的常量?
03 如何最簡單、通俗地理解C++的變數?
04 如何最簡單、通俗地理解C++的關鍵字?
05 如何最簡單、通俗地理解C++的識別符號?
06 如何最簡單、通俗地理解C++的資料型別?
07 如何最簡單、通俗地理解C++的轉義字元?
08 如何最簡單、通俗地理解C++的資料輸入?
09 如何最簡單、通俗地理解C++的運算子?
10 如何最簡單、通俗地理解C++的程式流程結構?
11 如何最簡單、通俗地理解C++的陣列?
12 如何最簡單、通俗地理解C++的函式?
13 如何最簡單、通俗地理解C++的指標?
14 如何最簡單、通俗地理解C++的結構體?
15 如何最簡單、通俗地理解C++的記憶體四區?
16 如何最簡單、通俗地理解C++的引用?
17 如何最簡單、通俗地理解C++的類和物件?
18 如何最簡單、通俗地理解C++的檔案互動?
19 如何最簡單、通俗地理解C++的模板?
20 如何最簡單、通俗地理解C++的STL?
21 如何最簡單、通俗地理解C++的string容器?
22 如何最簡單、通俗地理解C++的vector容器?
23 如何最簡單、通俗地理解C++的deque容器?
24 如何最簡單、通俗地理解C++的stack容器?
25 如何最簡單、通俗地理解C++的queue容器?
26 如何最簡單、通俗地理解C++的list容器?
27 如何最簡單、通俗地理解C++的set容器?
28 如何最簡單、通俗地理解C++的pair對組?
29 如何最簡單、通俗地理解C++的map容器?
30 如何最簡單、通俗地理解C++的函式物件?
31 如何最簡單、通俗地理解C++的謂詞?
32 如何最簡單、通俗地理解C++的內建函式物件?
33 C++的常用演算法有哪些?
34 C++的常用遍歷演算法有哪些?
35 C++的常用查詢演算法有哪些?
36 C++的常用排序演算法有哪些?
37 C++的常用拷貝和替換演算法有哪些?
38 C++的常用算術生成演算法有哪些?
39 C++的常用集合演算法有哪些?
[C++系列已完結]
( Python、機器視覺、爬蟲……系列筆記,見專欄 )
如何最簡單 通俗地理解C 的常量?
小王同學在積累 一 筆記 二 筆記目錄 常量,用於記錄程式中不可更改的資料。C 定義常量兩種方式,如下所示 1.define 巨集常量 define 常量名常量值 通常在檔案上方定義,表示乙個常量。2.const修飾的變數 const 資料型別常量名 常量值 通常在變數定義前加關鍵字const,修飾...
如何最簡單 通俗地理解C 的STL?
寫bug的程式設計師 簡單 通俗理解 那就是vector list unordered map iterator 把這些搞清楚,基本也就入門了,再慢慢看吧 後端技術小屋 學習乙個東西,首先要先用上,才能對它有乙個直觀感性的了解。在了解用法的基礎上再深挖個中原理才能有的放矢。STL是C 的乙個重要組成...
如何最簡單 通俗地理解C 的模板?
cstdio無敵曼巴 c 板子大全 歸併排序 include include using namespace std int a 100001 t 100001 void merge sort int l,int r int main 快速排序 include include using names...