本篇文章來給大家介紹下C++STL中String類一些比較常用的用法,
看完別忘了一鍵三連

文章目錄
- 簡介
- string物件的初始化方法
- string物件的賦值
- 從輸入流讀取字串
- 關系運算子的使用
- string類的遍歷以及迭代器的使用
- 將數值遍歷轉成string
- 字串流處理
- string類常用的內置方法
- 使用size方法獲取字串長度
- 使用assign方法進行賦值
- 使用append方法連接字串
- 使用compare方法比較string物件大小
- 使用substr獲得字串
- 使用find、rfind方法查找子串
- 使用find_first_not_of、find_last_not_of方法
- 使用erase方法洗掉字符
- 使用replace方法替換string物件中的字符
- 使用insert方法在string物件中插入字符
- 使用c_str獲得char*字串
- 使用algorithm中的函式處理字串
- 使用swap交換兩個字串的值
- 使用reverse函式逆置字串
- 使用sort函式對string進行排序
簡介
string類是C++STL庫(standard template library)中專門用來處理字串的,string類中提供了我們常用的字串處理方法(拼接,查找子串等),也多載了一些運算子(+、>、<、>=、<=等),
string類是一個模板類
typedef basic_string<char> string;
在使用string類是,需要包含頭檔案<string>
#include<string>
string物件的初始化方法
string物件的初始化主要有以下幾種方式,看代碼
#include<bits/stdc++.h>
using namespace std;
int main() {
// 表示一個空字串
string str;
// 建構式中直接傳入一個字串
string str1("str1");
// 也可以用=的方式進行賦值
string str2 = "str2";
// 表示str3為長度為8的字串,且都是字符'a'
string str3(8,'a');
// 講其他字串物件賦值給str4
string str4(str1.begin(), str1.end());
char s[20] = "hello world";
// 也可以傳入一個字串陣列
string str5(s);
// 用等號方式把字串陣列賦值給str6
string str6 = s;
return 0;
}
錯誤的初始化方法如下
#include<bits/stdc++.h>
using namespace std;
int main() {
string estr1 = 'a';
string estr2('b');
string estr3 = 3;
string estr4(4);
return 0;
}
string物件的賦值
#include<bits/stdc++.h>
using namespace std;
int main() {
string str;
str = '1';
cout << str << endl;
string str2 = "hello";
string str3 = "world";
// 將str直接賦值給str
str = str2;
cout << str << endl;
// 將str2和str3字串進行拼接后賦值給str
str = str2 + str3;
cout << str << endl;
string str4;
// 用assign方法將str2全部復制給str4
str4.assign(str2);
cout << str4 << endl;
// 從str2下標1開始復制2個字符給str5
string str5;
str5.assign(str2, 1, 2);
cout << str5 << endl;
return 0;
}
從輸入流讀取字串
#include<bits/stdc++.h>
using namespace std;
int main() {
string str;
// 可以讀取連續的字符,直到遇到空字符停止
cin >> str;
string str1;
// 可以讀取一整行字符,即遇到'\n'才停止
getline(cin, str1);
return 0;
}
關系運算子的使用
可以用關系運算子比較string物件的大小(== , >, >=, <, <=, !=)
這些關系運算子比較的結果也是回傳bool型別,true為成立,false為不成立,
#include<bits/stdc++.h>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "world";
cout << (str1 == str2) << endl;
cout << (str1 > str2) << endl;
cout << (str1 >= str2) << endl;
cout << (str1 < str2) << endl;
cout << (str1 <= str2) << endl;
cout << (str1 != str2) << endl;
return 0;
}
string類的遍歷以及迭代器的使用
要訪問順序容器和關聯容器中的元素,需要通過“迭代器(iterator)”進行,迭代器是一個變數,相當于容器和操縱容器的演算法之間的中介,迭代器可以指向容器中的某個元素,通過迭代器就可以讀寫它指向的元素,從這一點上看,迭代器和指標類似
獲取string類的迭代器如下
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
string::iterator it = str.begin();
return 0;
}
- str.begin()獲取字串第一個位置
- str.end()獲取字串最后一個字符的下一個位置,相當于
'\0'
利用迭代器遍歷整個字串
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
string::iterator it;
for (it = str.begin(); it != str.end(); it++) {
cout << *it << endl;
}
return 0;
}
遍歷字串除了用迭代器,也可以用遍歷字符陣列的方式,
string類內部多載了[],讓我們可以像訪問陣列一樣訪問string物件中的單個字符,
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
for (int i = 0; i < str.size();i++) {
cout << str[i] << endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main() {
string str1 = "test ";
string str2 = "output ";
int a = 1111;
ostringstream outputString;
outputString << str1 << str2 << a;
cout << outputString.str() << endl;
return 0;
}
將數值遍歷轉成string
可以將int、double、longlong等型別遍歷轉成字串
#include<bits/stdc++.h>
using namespace std;
int main() {
int a = 11;
double b = 22.22;
long long c = 33333;
string str1 = to_string(a);
string str2 = to_string(b);
string str3 = to_string(c);
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
字串流處理
除了標準流和檔案流輸入輸出外,還可以從string進行輸入輸出,具體實體如代碼所示
#include<bits/stdc++.h>
using namespace std;
int main() {
string inputStr = "input string 1 3.1415 a";
istringstream inputString(inputStr);
string str1, str2;
int a;
double b;
char c;
inputString >> str1 >> str2 >> a >> b >> c;
cout << str1 << endl;
cout << str2 << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
string類常用的內置方法
使用size方法獲取字串長度
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
cout << (str.size()) << endl;
return 0;
}
使用assign方法進行賦值
#include<bits/stdc++.h>
using namespace std;
int main() {
string str2 = "hello";
string str3 = "world";
string str4;
// 用assign方法將str2全部復制給str4
str4.assign(str2);
cout << str4 << endl;
// 從str2下標1開始復制2個字符給str5
string str5;
str5.assign(str2, 1, 2);
cout << str5 << endl;
return 0;
}
使用append方法連接字串
#include<bits/stdc++.h>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "world";
string str3 = "hello";
// 把s2拼接到str1
str1.append(str2);
cout << str1 << endl;
// 從下標2開始,把str2中4個字符拼接到str3后面,如果不夠,則到最后一次字符
str3.append(str2, 2, 4);
cout << str3 << endl;
return 0;
}
使用compare方法比較string物件大小
#include<bits/stdc++.h>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "world";
string str3 = "hello";
string str4 = "hewww";
cout << (str1.compare(str2)) << endl; // -1
cout << (str2.compare(str1)) << endl; // 1
cout << (str1.compare(str3)) << endl; // 0
cout << (str3.compare(str4)) << endl; // -1
// 相當于 "he"和"he"進行比較
cout << (str3.compare(0, 1, str4, 0, 1)) << endl; // 0
// 相當于 "www"和"world"進行比較
cout << (str4.compare(2, str4.size(), str2)) << endl; // 1
return 0;
}
使用substr獲得字串
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
string str1;
// 從下標1開始,5個字符
str1 = str.substr(1, 5);
cout << str1;
return 0;
}
使用find、rfind方法查找子串
- string::npos是string類中定義的靜態常量,如果找不到字串,則回傳string::npos
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
// find方法會從前往后找第一次出現目標串的位置,如果找到,則回傳起始下標,
cout << (str.find("lo")) << endl;
// rfind方法會從后往前找第一次出現目標串的位置,如果找到,則回傳起始下標,
cout << (str.rfind("o")) << endl;
// 從下標5的地方開始找字串"lo"
cout << (str.find("lo", 5)) << endl;
return 0;
}
使用find_first_not_of、find_last_not_of方法
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
// 從前向后查找不在 “aaa” 中的字母第一次出現的地方,如果找到,回傳找到字母的位置,如果找不到,回傳string::npos,
cout << (str.find_first_not_of("aaa")) << endl;
//從后向前查找不在 “abcd” 中的字母第一次出現的地方,如果找到,回傳找到字母的位置,如果找不到,回傳string::npos,
cout << (str.find_last_not_of("aaa")) << endl;
return 0;
}
使用erase方法洗掉字符
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
// 洗掉下標5以及之后的字串
str.erase(5);
cout << str << endl;
str = "hello world";
// 從下標5開始洗掉1個字串
str.erase(5, 1);
cout << str << endl;
str = "hello world";
// 從str.begin()+2地方開始洗掉兩個字符
str.erase(str.begin() + 2, str.begin() + 4);
cout << str << endl;
return 0;
}
使用replace方法替換string物件中的字符
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
// 將s1中下標0 開始的5個字符換成“nihao”
cout << (str.replace(0, 5, "nihao")) << endl;
return 0;
}
使用insert方法在string物件中插入字符
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
string str1 = "test insert";
// 將str1插入str下標4的位置
cout << (str.insert(4, str1));
return 0;
}
使用c_str獲得char*字串
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
printf("%s", str.c_str());
return 0;
}
使用algorithm中的函式處理字串
使用swap交換兩個字串的值
#include<bits/stdc++.h>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "world";
swap(str1, str2);
cout << str1 << endl;
cout << str2 << endl;
return 0;
}
使用reverse函式逆置字串
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "hello world";
reverse(str.begin(), str.end());
cout << str;
return 0;
}
使用sort函式對string進行排序
#include<bits/stdc++.h>
using namespace std;
int main() {
string str = "advadfgasdfa";
sort(str.begin(), str.end());
cout << str << endl;
return 0;
}
拒絕白嫖,從點一鍵三連做起
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/258055.html
標籤:其他
