目錄
- 1、C++11的string類
- 1、創建 string 對象
- 2、追加字符串append函數
- 3、為字符串賦值assign函數
- 4、at, clear, erase, and empty函數
- 5、比較字符串compare()
- 6、獲取子串at() 、substr()函數
- 7、搜索字符串find()
- 8、插入和替換字符串insert() 、replace()
- 9、字符串運算符
- 10、string_view與string的幾點差別
- 2、C++11的數組類
- 1. C-Style Array v.s. C++ Style Array (C風格數組和C++風格數組)
- 1、C Style Array (C++ raw array,也叫做C++原生數組)
- 2、C Style Array (C++ raw array,也叫做C++原生數組)
- 2. Create C++ Style Array (創建C++風格數組)
- 3.std::array的成員函數
C++ 使用 string 類處理字符串
string類中的函數
(1) 構造
(2) 追加
(3) 賦值
(4) 位置與清除
(5) 長度與容量
(6) 比較
(7) 子 串
(8) 搜索
(9) 運算符
1、C++11的string類
1、創建 string 對象
由一個字符串常量或字符串數組創建string對象
string message{ "Aloha World!" };
char charArray[] = {'H', 'e', 'l', 'l', 'o', '\0'};
string message1{ charArray };
2、追加字符串append函數
一系列的重載函數可以將新內容附加到一個字符串中
string s1{ "Welcome" };
s1.append( " to C++" ); // appends " to C++" to s1
cout << s1 << endl; // s1 now becomes Welcome to C++string s2{ "Welcome" };
s2.append( " to C and C++", 3, 2 ); // appends " C" to s2
cout << s2 << endl; // s2 now becomes Welcome Cstring s3{ "Welcome" };
s3.append( " to C and C++", 5); // appends " to C" to s3
cout << s3 << endl; // s3 now becomes Welcome to Cstring s4{ "Welcome" };
s4.append( 4, 'G' ); // appends "GGGG" to s4
cout << s4 << endl; // s4 now becomes WelcomeGGGG
3、為字符串賦值assign函數
一系列的重載函數可以將一個字符串賦以新內容
string s1{ "Welcome" };
s1.assign( "Dallas" ); // assigns "Dallas" to s1
cout << s1 << endl; // s1 now becomes Dallasstring s2{ "Welcome" };
s2.assign( "Dallas, Texas", 1, 3 ); // assigns "all" to s2
cout << s2 << endl; // s2 now becomes allstring s3{ "Welcome" };
s3.assign( "Dallas, Texas", 6 ); // assigns "Dallas" to s3
cout << s3 << endl; // s3 now becomes Dallasstring s4{ "Welcome" };
s4.assign( 4, 'G' ); // assigns "GGGG" to s4
cout << s4 << endl; // s4 now becomes GGGG
4、at, clear, erase, and empty函數
(1) at(index): 返回當前字符串中index位置的字符
(2) clear(): 清空字符串
(3) erase(index, n): 刪除字符串從index開始的n個字符
(4) empty(): 檢測字符串是否為空
string s1{ "Welcome" };cout << s1.at(3) << endl; // s1.at(3) returns ccout << s1.erase(2, 3) << endl; // s1 is now Wemes1.clear(); // s1 is now emptycout << s1.empty() << endl; // s1.empty returns 1 (means true)
5、比較字符串compare()
compare() 函數用于比較兩個字符串。它與C語言中的 strcmp() 函數很像。
string s1{ "Welcome" };
string s2{ "Welcomg" };
cout << s1.compare(s2) << endl; // returns -2
cout << s2.compare(s1) << endl; // returns 2
cout << s1.compare("Welcome") << endl; // returns 0
6、獲取子串at() 、substr()函數
string s1{ "Welcome" };
cout << s1.substr(0, 1) << endl; // returns W; 從0號位置開始的1個字符
cout << s1.substr(3) << endl; // returns come; 從3號位置直到末尾的子串
cout << s1.substr(3, 3) << endl; // returns com;從3號位置開始的3個字符
7、搜索字符串find()
find() 函數可以在一個字符串中搜索一個子串或者一個字符
string s1{ "Welcome to C++" };
cout << s1.find("co") << endl; // returns 3; 返回子串出現的第一個位置
cout << s1.find("co", 6) << endl; // returns -1 從6號位置開始查找子串出現的第一個位置
cout << s1.find('o') << endl; // returns 4 返回字符出現的第一個位置
cout << s1.find('o', 6) << endl; // returns 9 從6號位置開始查找字符出現的第一個位置
8、插入和替換字符串insert() 、replace()
string s1("Welcome to C++");
s1.insert(11, "Java and ");
cout << s1 << endl; // s1 becomes Welcome to Java and C++string s2{ "AA" };
s2.insert(1, 4, 'B'); //在1號位置處連續插入4個相同字符
cout << s2 << endl; // s2 becomes to ABBBBAstring s3{ "Welcome to Java" };
s3.replace(11, 4, "C++"); //從11號位置開始向后的4個字符替換掉。注意'\0'
cout << s3 << endl; // returns Welcome to C++
9、字符串運算符
string s1 = "ABC"; // The = operatorstring s2 = s1; // The = operatorfor (int i = s2.size() - 1; i >= 0; i--)cout << s2[i]; // The [] operatorstring s3 = s1 + "DEFG"; // The + operatorcout << s3 << endl; // s3 becomes ABCDEFGs1 += "ABC";cout << s1 << endl; // s1 becomes ABCABCs1 = "ABC";s2 = "ABE";cout << (s1 == s2) << endl; // Displays 0 cout << (s1 != s2) << endl; // Displays 1 cout << (s1 > s2) << endl; // Displays 0 cout << (s1 >= s2) << endl; // Displays 0 cout << (s1 < s2) << endl; // Displays 1 cout << (s1 <= s2) << endl; // Displays 1
10、string_view與string的幾點差別
C++中的string類與Java中的String類不同。為了有一個和Java中的String類特性類似的東西,C++17中提供了string_view類。請你查查資料,說說string_view與string的幾點差別。
C++的string對象,如果大于默認的字符串長度閥值。對于長度為N的字符串,時間成本為O(n),空間成本是2xS(n);
于是C++17就有了string_view這個標準庫的擴展,這個擴展極大地解決了string拷貝的空間成本和時間成本問題。
string_view基本沒有涉及內存的額外分配。
string_view 是C++17所提供的用于處理只讀字符串的輕量對象。這里后綴 view 的意思是只讀的視圖。
通過調用 string_view 構造器可將字符串轉換為 string_view 對象。string 可隱式轉換為 string_view。string_view 是只讀的輕量對象,它對所指向的字符串沒有所有權。string_view通常用于函數參數類型,可用來取代 const char* 和 const string&。string_view 代替 const string&,可以避免不必要的內存分配。string_view的成員函數即對外接口與 string 相類似,但只包含讀取字符串內容的部分。string_view::substr()的返回值類型是string_view,不產生新的字符串,不會進行內存分配。string::substr()的返回值類型是string,產生新的字符串,會進行內存分配。string_view字面量的后綴是 sv。(string字面量的后綴是 s)
string_view的適用場合
(由于string_view對象無法被使用它的函數修改,因此要更新string_view所引用的字符串副本,還是需要修改它所引用的string類型的內部字符串副本。)
字符串查找 遍歷字符串 顯示字符串
2、C++11的數組類
1. C-Style Array v.s. C++ Style Array (C風格數組和C++風格數組)
1、C Style Array (C++ raw array,也叫做C++原生數組)
特點:
int arr[ ] = { 1, 2, 3 };
arr 可能會退化為指針:void f(int a[]) { std::cout << sizeof(a)/sizeof(a[0]); }
arr 不知道自己的大小: sizeof(arr)/sizeof(arr[0])
兩個數組之間無法直接賦值: array1 = array2;
不能自動推導類型:auto a1[] = {1,2,3};
2、C Style Array (C++ raw array,也叫做C++原生數組)
特點;
是一個容器類,所以有迭代器(可以認為是一種用于訪問成員的高級指針)
可直接賦值
知道自己大小:size()
能和另一個數組交換內容:swap()
能以指定值填充自己: fill()
取某個位置的元素( 做越界檢查) :at()
2. Create C++ Style Array (創建C++風格數組)
#include
std::array< 數組 類型, 數組大小> 數組名字;
std::array< 數組 類型, 數組大小> 數組 名字 { 值1, 值2, …};限制與C風格數組相同 std::array<int , 10> x;
std::array<char , 5> c{ ‘H’,‘e’,‘l’,‘l’,‘o’ };
3.std::array的成員函數