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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++ 笔记(22)— STL string 类(字符串赋值、访问、拼接、查找、翻转、大小写转换)

發布時間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 笔记(22)— STL string 类(字符串赋值、访问、拼接、查找、翻转、大小写转换) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 實例化和賦值 STL string

#include <string>
#include <iostream>int main ()
{using namespace std;const char* a = "Hello String!";cout << "a is: " << a << endl;std::string b(a); // constructorcout << "b is: " << b << endl;std::string c("Hello String!");std::string d = c;cout << "d is: " << d << endl;// Initialize a string to the first 5 characters of anotherstd::string e(a, 5);cout << "e is: " << e << endl;// Initialize a string object to contain 10 'a'sstd::string f(10, 'a');cout << "f is: " << f << endl;return 0;
}

輸出結果:

a is: Hello String!
b is: Hello String!
d is: Hello String!
e is: Hello
f is: aaaaaaaaaa
#include <iostream>
#include <string>using namespace std;int main()
{// 實例化并初始化string 對象時,無需關心字符串長度和內存分配細節。// STL string 類的構造函數將自動完成這些工作。std::string a = "hello, world";string b = a;std::string c (a, 3);// string d (10, "a");cout << "a: " << a << endl;cout << "b: " << b << endl;cout << "c: " << c << endl;// cout << "d: " << d << endl;return 0;
}

2. 訪問std::string 的字符內容

要訪問 STL string 的字符內容,可使用迭代器,也可采用類似于數組的語法并使用下標運算符 [] 提供偏移量。要獲得 string 對象的 C 風格表示,可使用成員函數 c_str()

#include <string>
#include <iostream>int main ()
{using namespace std;string a ("Hello String"); // sample// Access the contents of the string using array syntaxcout << "Display elements in string using array-syntax: " << endl;for (size_t i = 0; i < a.length(); ++i){cout << "i [" << i << "] is: ";cout << a [i] << endl;}cout << endl;// Access the contents of a string using iteratorscout << "Display elements in string using iterators: " << endl;int j = 0;string::const_iterator k;for (auto k = a.cbegin(); k != a.cend (); ++ k){cout << "k [" << j ++ << "] is: ";cout << *k << endl;}cout << endl;// Access contents as a const char*cout << "The char* representation of the string is: ";cout << a.c_str () << endl;return 0;
}

3. 拼接字符串

要拼接字符串,可使用運算符 += ,也可使用成員函數 append()

#include <string>
#include <iostream>using namespace std;int main ()
{string a ("hello ");string b = "world";string c;c = a + b;string d;d.append(a);cout << "a is: " << a << endl;  // a is: hello cout << "b is: " << b << endl;  // b is: worldcout << "c is: " << c << endl;  // c is: hello worldcout << "d is: " << d << endl;  // d is: hello return 0;
}

4. 在 string 中查找字符或子字符串

STL string 類提供了成員函數 find() ,該函數有多個重載版本,可在給定 string 對象中查找字符或子字符串。

#include <string>
#include <iostream>
using namespace std;int main ()
{string a ("Good day!");cout << "a is:" << a << endl;// Find substring "day" - find() returns positionsize_t charPos = a.find("day", 0);// Check if the substring was found...if (charPos != string::npos)cout << "First instance \"day\" at pos. " << charPos << endl;elsecout << "Substring not found." << endl;cout << "Locating all instances of substring \"day\"" << endl;size_t subStrPos = a.find ("day", 0);while (subStrPos != string::npos){cout << "\"day\" found at position " << subStrPos << endl;// Make find() search forward from the next character onwardssize_t searchOffset = subStrPos + 1;subStrPos = a.find ("day", searchOffset);}return 0;
}

第 11~17 行演示了 find() 函數的最簡單用法,它判斷在 string 中是否找到了特定子字符串。這是通過將 find() 操作的結果與 std::string::npos (實際值為?1)進行比較實現的, std::string::npos 表明沒有找到要搜索的元素。如果 find() 函數沒有返回 npos ,它將返回一個偏移量,指出子字符串或字符在 string 中的位置。

這段代碼演示了如何在 while 循環中使用 find() 函數在 STL string 查找指定字符或子字符串的所有實例。這里使用的 find 函數的重載版本接受兩個參數:要搜索的子字符串或字符以及命令 find() 從哪里開始搜索的偏移量。在第 29 行,我們就通過指定偏移量,讓 find() 搜索下一個指定的子字符串。

5. 字符串翻轉

有時需要反轉字符串的內容。假設要判斷用戶輸入的字符串是否為回文,方法之一是將其反轉,再與原來的字符串進行比較。反轉 STL string 很容易,只需使用泛型算法 std::reverse()

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;int main ()
{string a ("Hello String! We will reverse you!");cout << "The original sample string is: " << endl;cout << a << endl << endl;reverse (a.begin(), a.end());cout << "After applying the std::reverse algorithm: " << endl;cout << a << endl;return 0;
}

第 12 行的 std::reverse() 算法根據兩個輸入參數指定的邊界反轉邊界內的內容。在這里,兩個邊界分別是 string 對象的開頭和末尾,因此整個字符串都被反轉。只要提供合適的輸入參數,也可將字符串的一部分反轉。注意,邊界不能超過 end()

6. 字符串的大小寫轉換

要對字符串進行大小寫轉換,可使用算法 std::transform() ,它對集合中的每個元素執行一個用戶指定的函數。在這里,集合是 string 對象本身。

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;int main ()
{string a = "hello";transform(a.begin(), a.end(), a.begin(), ::toupper);cout << "The string converted to upper case is: " << endl;cout << a << endl << endl;transform(a.begin(), a.end(), a.begin(), ::tolower);cout << "The string converted to lower case is: " << endl;cout << a << endl << endl;return 0;
}

總結

以上是生活随笔為你收集整理的C++ 笔记(22)— STL string 类(字符串赋值、访问、拼接、查找、翻转、大小写转换)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。