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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

深入浅出之string

發布時間:2024/9/21 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深入浅出之string 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

string 類是?STL?中 basic_string 模板實例化得到的模板類。其定義如下:

typedef basic_string <char> string;

1. 定義及初始化string對象

string 類有多個構造函數,用法示例如下:

1) string()字符串為空值 如:string s1(); // si = "" 2)string(const char *str) 字符串為str中的字符 如:string s2("Hello"); // s2 = "Hello" 3)string(size_type length,char ch) 如:string s3(4, 'K'); // s3 = "KKKK" 4)string(const char *str,size_type index,size_type length) 字符串值為str以index開始的length個字符 如:string s4("12345", 1, 3); //s4 = "234",即 "12345" 的從下標 1 開始,長度為 3 的子串 5)string(const char *str,size_type index)字符串值為str第index開始的所有字符 如:string S1(“Strings”,8)

注意:?string 類沒有接收一個整型參數或一個字符型參數的構造函數。下面的兩種寫法是錯誤的:

string s1('K'); string s2(123);

2. 字符串賦值

2.1 assign函數?

string s1("12345"), s2; s3.assign(s1); // s3 = s1 s2.assign(s1, 1, 2); // s2 = "23",即 s1 的子串(1, 2) s2.assign(4, 'K'); // s2 = "KKKK" s2.assign("abcde", 2, 3); // s2 = "cde",即 "abcde" 的子串(2, 3)

2.2 賦值運算符=?

string類已經對“=”進行了重載,可以用 char* 類型的變量、常量,以及 char 類型的變量、常量對 string 對象進行賦值。例如:

string s1; s1 = "Hello"; // s1 = "Hello" s2 = 'K'; // s2 = "K”

3. 字符串比較

?3.1 關系運算符

? ? ==,>,<,>=,<=,!=都已經重載,對字符串的操作可以適用。

?3.2 compare函數

?compare 成員函數有以下返回值:

  • 小于 0 表示當前的字符串小;
  • 等于 0 表示兩個字符串相等;
  • 大于 0 表示另一個字符串小。
string s1("hello"), s2("hello, world"); int n = s1.compare(s2); n = s1.compare(1, 2, s2, 0, 3); //比較s1的子串 (1,2) 和s2的子串 (0,3) n = s1.compare(0, 2, s2); // 比較s1的子串 (0,2) 和 s2 n = s1.compare("Hello"); n = s1.compare(1, 2, "Hello"); //比較 s1 的子串(1,2)和"Hello” n = s1.compare(1, 2, "Hello", 1, 2); //比較 s1 的子串(1,2)和 "Hello" 的子串(1,2

4. 字符串的連接

?4.1 append函數

string s1("123"), s2("abc"); s1.append(s2); // s1 = "123abc" s1.append(s2, 1, 2); // s1 = "123abcbc" s1.append(3, 'K'); // s1 = "123abcbcKKK" s1.append("ABCDE", 2, 3); // s1 = "123abcbcKKKCDE",添加 "ABCDE" 的子串(2, 3)

4.2 =+符合賦值運算符?

string類已經將+運算符重載,可以對字符串進行連接

string s1(“hello”),s2(“world”); s1 = s1+s2;

5. 字符串特性

5.1 獲取字符串大小size或length

#include <QCoreApplication> #include <string> #include <iostream>using namespace std;int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);string s,s1,s2;s1 = "hello";s2 = "world";s = s1+" "+s2;qDebug("%s-%d-%d",s.c_str(),s.length(),s.size());return a.exec(); }

5.2 獲取字符容量

int capacity() const;

5.3 設置字符串長度resize

void resize(int len, char c);?

#include <QCoreApplication> #include <string> #include <iostream>using namespace std;int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);string s,s1,s2;s1 = "hello";s2 = "world";s = s1+" "+s2;s.resize(12,'1');qDebug("%s-%d-%d",s.c_str(),s.length(),s.size());return a.exec(); } 輸出結果: hello world1-12-12

6. 字符串操作?

6.1 取得字符串中的字符 []

char& operator[](int n)

string s("hello world"); char c = s[1];

?6.2 取得字符串中的字符 at

char& at(int n)

string s("hello world"); char c = s.at(1);

注意:

【】和at區別在于,at函數提供范圍檢測,越界時會拋出異常;而【】不會提供下標檢測。?

6.3? 獲得一個字符串 c_str

const char *c_str()const;

返回一個以null終止的字符串

字符數組轉化成string類型
char ch [] = “ABCDEFG”;
string str(ch);//也可string str = ch;
或者
char ch [] = “ABCDEFG”;
string str;
str = ch;//在原有基礎上添加可以用str += ch;

將string類型轉換為字符數組
char buf[10];
string str(“ABCDEFG”);
length = str.copy(buf, 9);
buf[length] = ‘\0’;
或者
char buf[10];
string str(“ABCDEFG”);
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 10);

6.4?求 string 對象的子串

string substr(int n = 0, int m = string::npos) const;

string s1 = "this is ok"; string s2 = s1.substr(2, 4); // s2 = "is i" s2 = s1.substr(2); // s2 = "is is ok"

6.5?交換兩個string對象的內容

swap 成員函數可以交換兩個 string 對象的內容。例如:

string s1("West”), s2("East"); s1.swap(s2); // s1 = "East",s2 = "West"

?6.6?查找子串和字符

string 類有一些查找子串和字符的成員函數,它們的返回值都是子串或字符在 string 對象字符串中的位置(即下標)。如果查不到,則返回 string::npos。string: :npos 是在 string 類中定義的一個靜態常量。這些函數如下:

  • find:從前往后查找子串或字符出現的位置。
  • rfind:從后往前查找子串或字符出現的位置。
  • find_first_of:從前往后查找何處出現另一個字符串中包含的字符。例如:
  • s1.find_first_of("abc");? //查找s1中第一次出現"abc"中任一字符的位置
  • find_last_of:從后往前查找何處出現另一個字符串中包含的字符。
  • find_first_not_of:從前往后查找何處出現另一個字符串中沒有包含的字符。
  • find_last_not_of:從后往前查找何處出現另一個字符串中沒有包含的字符。
#include <QCoreApplication> #include <string> #include <iostream>using namespace std; int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);string s1("Source Code");int n;if ((n = s1.find('u')) != string::npos) //查找 u 出現的位置cout << "1) " << n << "," << s1.substr(n) << endl;//輸出 l)2,urce Codeif ((n = s1.find("Source", 3)) == string::npos)//從下標3開始查找"Source",找不到cout << "2) " << "Not Found" << endl; //輸出 2) Not Foundif ((n = s1.find("Co")) != string::npos)//查找子串"Co"。能找到,返回"Co"的位置cout << "3) " << n << ", " << s1.substr(n) << endl;//輸出 3) 7, Codeif ((n = s1.find_first_of("ceo")) != string::npos)//查找第一次出現或 'c'、'e'或'o'的位置cout << "4) " << n << ", " << s1.substr(n) << endl;//輸出 4) l, ource Codeif ((n = s1.find_last_of('e')) != string::npos)//查找最后一個 'e' 的位置cout << "5) " << n << ", " << s1.substr(n) << endl; //輸出 5) 10, eif ((n = s1.find_first_not_of("eou", 1)) != string::npos)//從下標1開始查找第一次出現非 'e'、'o' 或 'u' 字符的位置cout << "6) " << n << ", " << s1.substr(n) << endl;//輸出 6) 3, rce Codereturn a.exec(); }

6.7?替換子串

string s1("Real Steel"); s1.replace(1, 3, "123456", 2, 4); //用 "123456" 的子串(2,4) 替換 s1 的子串(1,3) cout << s1 << endl; //輸出 R3456 Steel string s2("Harry Potter"); s2.replace(2, 3, 5, '0'); //用 5 個 '0' 替換子串(2,3) cout << s2 << endl; //輸出 HaOOOOO Potter int n = s2.find("OOOOO"); //查找子串 "00000" 的位置,n=2 s2.replace(n, 5, "XXX"); //將子串(n,5)替換為"XXX" cout << s2 < < endl; //輸出 HaXXX Potter

6.8 刪除子串?

string s1("Real Steel"); s1.erase(1, 3); //刪除子串(1, 3),此后 s1 = "R Steel" s1.erase(5); //刪除下標5及其后面的所有字符,此后 s1 = "R Ste"

?6.9 插入子串

string s1("Limitless"), s2("00"); s1.insert(2, "123"); //在下標 2 處插入字符串"123",s1 = "Li123mitless" s1.insert(3, s2); //在下標 2 處插入 s2 , s1 = "Li10023mitless" s1.insert(3, 5, 'X'); //在下標 3 處插入 5 個 'X',s1 = "Li1XXXXX0023mitless"

7. string與各類型轉換

待補充

參考資料

  • http://c.biancheng.net/view/400.html

總結

以上是生活随笔為你收集整理的深入浅出之string的全部內容,希望文章能夠幫你解決所遇到的問題。

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