數字和字符串相互轉換
#include <string>
#include <sstream>
int main(){
double a = 123.32;
string res;
stringstream ss;
ss << a;
ss >> res;//或者 res = ss.str();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
字符串轉數字,int float類型 同理
int main(){
string a = "123.32";
double res;
stringstream ss;
ss << a;
ss >> res;
return 0;
}
1
2
3
4
5
6
7
8
一、初始化
初始化有兩種方式,其中使用等號的是拷貝初始化,不使用等號的是直接初始化。
string str1 = "hello world"; // str1 = "hello world"
string str2("hello world"); // str2 = "hello world"
string str3 = str1; // str3 = "hello world"
string str4(str2); // str4 = "hello world"
string str5(10,'h'); // str5 = "hhhhhhhhhh"
string str6 = string(10,'h'); // str6 = "hhhhhhhhhh"
string str7(str1,6); // str7 = "world" 從字符串str1第6個字符開始到結束,拷貝到str7中
string str8 = string(str1,6); // str8 = "world"
string str9(str1,0,5); // str9 = "hello" 從字符串str1第0個字符開始,拷貝5個字符到str9中
string str10 = string(str1,0,5); // str10 = "hello"
char c[] = "hello world";
string str11(c,5); // str11 = "hello" 將字符數組c的前5個字符拷貝到str11中
string str12 = string(c,5); // str12 = "hello"
值得一提的是,如果:
string str13 = string("hello world",5) // str13 = "hello" 而非 " world"
此時,"hello world"應看作字符數組(參見str11),而非string對象(參見str7)
爲了避免發生意外,在字符串插入、替換、添加、賦值、比較中去除了關於後一種的相關操作(參見後文)。
二、獲取長度(length、size)
length()函數與size()函數均可獲取字符串長度。
string str = "hello world";
cout << str.length() << str.size();
當str.length()與其他類型比較時,建議先強制轉換爲該類型,否則會意想之外的錯誤。
比如:-1 > str.length() 返回 true。
三、插入(insert)
基本情況爲以下四種,其餘變形函數自行摸索即可。
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.insert(pos,n,ch) 在字符串s的pos位置上面插入n個字符ch
str.insert(6,4,'z'); // str = "hello zzzzworld"
//s.insert(pos,str) 在字符串s的pos位置插入字符串str
str.insert(6,str2); // str = "hello hard world"
//s.insert(pos,str,a,n) 在字符串s的pos位置插入字符串str中位置a到後面的n個字符
str.insert(6,str3,6,9); // str = "hello so happy world"
//s.insert(pos,cstr,n) 在字符串s的pos位置插入字符數組cstr從開始到後面的n個字符
//此處不可將"it is so happy wow"替換爲str3
str.insert(6,"it is so happy wow",6); // str = "hello it is world"
四、替換(replace)
替換與插入對應,對比理解更爲簡單。
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
//s.replace(p0,n0,n,ch) 刪除p0開始的n0個字符,然後在p0處插入n個字符ch
str.replace(0,6,4,'z'); // str = "zzzzworld"
//s.replace(p0,n0,str) 刪除從p0開始的n0個字符,然後在p0處插入字符串str
str.replace(0,6,str2); // str = "hard world"
//s.replace(p0,n0,str,pos,n) 刪除p0開始的n0個字符,然後在p0處插入字符串str中從pos開始的n個字符
str.replace(0,6,str3,6,9); // str = "so happy world"
//s.replace(p0,n0,cstr,n) 刪除p0開始的n0個字符,然後在p0處插入字符數組cstr的前n個字符
//此處不可將"it is so happy wow"替換爲str3
str.replace(0,6,"it is so happy wow",6); // str = "it is world"
五、添加(append)
append函數用在字符串的末尾添加字符和字符串。(同樣與插入、替換對應理解)
string str = "hello world";
string str2 = "hard ";
string str3 = "it is so happy wow";
int x = 3;
str+=to_string(3) //數字和字符串連接
//s.append(n,ch) 在當前字符串結尾添加n個字符c
str.append(4,'z'); // str = "hello worldzzzz"
//s.append(str) 把字符串str連接到當前字符串的結尾
str.append(str2); // str = "hello worldhard "
//s.append(str,pos,n) 把字符串str中從pos開始的n個字符連接到當前字符串的結尾
str.append(str3,6,9); // str = "hello worldso happy "
//append(cstr,int n) 把字符數組cstr的前n個字符連接到當前字符串結尾
//此處不可將"it is so happy wow"替換爲str3
str.append("it is so happy wow",6); // str = "hello worldit is "
六、賦值(assign)
賦值也是一種初始化方法,與插入、替換、添加對應理解較爲簡單。
string str;
string temp = "welcome to my blog";
//s.assign(n,ch) 將n個ch字符賦值給字符串s
str.assign(10,'h'); // str = "hhhhhhhhhh"
//s.assign(str) 將字符串str賦值給字符串s
str.assign(temp); // str = "welcome to my blog"
//s.assign(str,pos,n) 將字符串str從pos開始的n個字符賦值給字符串s
str.assign(temp,3,7); // str = "come to"
//s.assaign(cstr,n) 將字符數組cstr的前n個字符賦值給字符串s
//此處不可將"it is so happy wow"替換爲temp
str.assign("welcome to my blog",7); //welcome
七、刪除(erase)
string str = "welcome to my blog";
//s.erase(pos,n) 把字符串s從pos開始的n個字符刪除
str.erase(11,3); // str = "welcome to blog"
八、剪切(substr)
string str = "The apple thinks apple is delicious";
//s.substr(pos,n) 得到字符串s位置爲pos後面的n個字符組成的串
string s1 = str.substr(4,5); // s1 = "apple"
//s.substr(pos) 得到字符串s從pos到結尾的串
string s2 = str.substr(17); // s2 = "apple is delicious"
九、比較(compare)
兩個字符串自左向右逐個字符相比(按ASCII值大小相比較),直到出現不同的字符或遇’\0’爲止。
若是遇到‘\0’結束比較,則長的子串大於短的子串,如:“9856” > “985”。
如果兩個字符串相等,那麼返回0,調用對象大於參數返回1,小於返回-1。
string str1 = "small leaf";
string str2 = "big leaf";
//s.compare(str) 比較當前字符串s和str的大小
cout << str1.compare(str2); // 1
//s.compare(pos,n,str) 比較當前字符串s從pos開始的n個字符與str的大小
cout << str1.compare(2,7,str2); // -1
//s.compare(pos,n0,str,pos2,n) 比較當前字符串s從pos開始的n0個字符與str中pos2開始的n個字符組成的字符串的大小
cout << str1.compare(6,4,str2,4,4); // 0
//s.compare(pos,n0,cstr,n) 比較當前字符串s從pos開始的n0個字符與字符數組cstr中前n個字符的大小
//此處不可將"big leaf"替換爲str2
cout << str1.compare(6,4,"big leaf",4); // 1
十、交換(swap)
交換兩個字符串的值。
string str1 = "small leaf";
string str2 = "big leaf";
//或者str1.swap(str2) ,輸出結果相同
swap(str1,str2); // str1 = "big leaf" str2 = "small leaf"
swap(str1[0],str1[1]); // str1 = "ibg leaf"
十一、反轉(reverse)
反轉字符串。
string str = "abcdefghijklmn";
reverse(str.begin(),str.end()); // str = "nmlkjihgfedcba"
十二、數值轉化(sto*)
詳情參見前篇文章《int、string 類型相互轉換》。
(本小白調試時,只有將p設置爲0或nullptr才運行成功)
to_string(val) //把val轉換成string
stoi(s,p,b) //把字符串s從p開始轉換成b進制的int
stol(s,p,b) //把字符串s從p開始轉換成b進制的long
stoul(s,p,b) //把字符串s從p開始轉換成b進制的unsigned long
stoll(s,p,b) //把字符串s從p開始轉換成b進制的long long
stoull(s,p,b) //把字符串s從p開始轉換成b進制的unsigned long long
stof(s,p) //把字符串s從p開始轉換成float
stod(s,p) //把字符串s從p開始轉換成double
stold(s,p) //把字符串s從p開始轉換成long double
十三、迭代器(iterator)
詳情參見下篇文章《string類型中迭代器的使用》。
string str = "abcdefghijklmn";
//s.begin() 返回字符串s第一個字符的位置
char a = *(str.begin()); // a
//s.end() 返回字符串s最後一個字符串的後一個位置
char b = *(str.end()-1); // n
//s.rbegin() 返回字符串s最後一個字符的位置
char c = *(str.rbegin()); // n
//s.rend() 返回字符串s第一個字符的前一個位置
char d = *(str.rend()-1); // a
十四、查找(find)
14.1 find函數
string str = "The apple thinks apple is delicious"; //長度34
string key = "apple";
//s.find(str) 查找字符串str在當前字符串s中第一次出現的位置
int pos1 = str.find(key); // 4
//s.find(str,pos) 查找字符串str在當前字符串s的[pos,end]中第一次出現的位置
int pos2 = str.find(key, 10); // 17
//s.find(cstr,pos,n) 查找字符數組cstr前n的字符在當前字符串s的[pos,end]中第一次出現的位置
//此處不可將"delete"替換爲str2(如果定義str2 = "delete")
int pos3 = str.find("delete", 0, 2); // 26
//s.find(ch,pos) 查找字符ch在當前字符串s的[pos,end]中第一次出現的位置
int pos4 = str.find('s', 0); // 15
14.2 rfind函數
//s.rfind(str) 查找字符串str在當前字符串s中最後一次出現的位置
int pos5 = str.rfind(key); // 17
//s.rfind(str,pos) 查找字符串str在當前字符串s的[0,pos+str.length()-1]中最後一次出現的位置
int pos6 = str.rfind(key, 16); // 4
//s.rfind(cstr,pos,n) 查找字符數組cstr前n的字符在當前字符串s的[0,pos+n-1]中最後一次出現的位置
//此處不可將"apple"替換爲key
int pos7 = str.rfind("apple", 40, 2); // 17
//s.rfind(ch.pos) 查找字符ch在當前字符串s的[0,pos]中最後一次出現的位置
int pos8 = str.rfind('s', 30); // 24
14.3 find_xxx_of函數
string str = "The early birds catch the warm"; //長度30
string key = "aeiou";
1
2
find_first_of
// s.find_first_of(str) 查找字符串str中的任意字符在當前字符串s中第一次出現的位置
int pos1 = str.find_first_of(key); // 2
//s.find_first_of(str,pos) 查找字符串str中的任意字符在當前字符串s的[pos,end]中第一次出現的位置
int pos2 = str.find_first_of(key, 10); // 11
//s.find_first_of(cstr,pos,n) 查找字符串str前n個任意字符在當前字符串s的[pos,end]中第一次出現的位置
//此處不可將"aeiou"替換爲key
int pos3 = str.find_first_of("aeiou", 7, 2); // 17
//s.find_first_of(ch,pos) 查找字符ch在當前字符串s的[pos,end]中第一次出現的位置
int pos4 = str.find_first_of('r', 0); // 6
find_first_not_of
//s.find_first_not_of(str) 查找字符串str之外的任意字符在當前字符串s中第一次出現的位置
int pos1 = str.find_first_not_of(key); // 0
//s.find_first_not_of(str,pos) 查找字符串str之外的任意字符在當前字符串s的[pos,end]中第一次出現的位置
int pos2 = str.find_first_not_of(key, 10); // 10
//s.find_first_not_of(cstr,pos,n) 查找字符串str前n個之外任意字符在當前字符串s的[pos,end]中第一次出現的位置
//此處不可將"aeiou"替換爲key
int pos3 = str.find_first_not_of("aeiou", 7, 2); // 7
//s.find_first_not_of(str) 查找字符ch之外任意字符在當前字符串s的[pos,end]中第一次出現的位置
int pos4 = str.find_first_not_of('r', 0); // 0
find_last_of
//s.find_last_of(str) 查找字符串str中的任意字符在當前字符串s中最後一次出現的位置
int pos1 = str.find_last_of(key); // 27
//s.find_last_of(str,pos) 查找字符串str中的任意字符在當前字符串s的[0,pos]中最後一次出現的位置
int pos2 = str.find_last_of(key, 15); // 11
//s.find_last_of(cstr,pos,n) 查找字符串str前n個任意字符在當前字符串s的[0,pos]中最後一次出現的位置
//此處不可將"aeiou"替換爲key
int pos3 = str.find_last_of("aeiou", 20, 2); // 17
//s.find_last_of(str) 查找字符ch在當前字符串s的[0,pos]中最後一次出現的位置
int pos4 = str.find_last_of('r', 30); // 28
find_last_not_of
//s.find_last_not_of(str) 查找字符串str之外的任意字符在當前字符串s中最後一次出現的位置
int pos1 = str.find_last_not_of(key); // 29
//s.find_last_not_of(str,pos) 查找字符串str之外的任意字符在當前字符串s的[0,pos]中最後一次出現的位置
int pos2 = str.find_last_not_of(key, 15); // 15
//s.find_last_not_of(cstr,pos,n) 查找字符串str前n個之外任意字符在當前字符串s的[0,pos]中最後一次出現的位置
//此處不可將"aeiou"替換爲key
int pos3 = str.find_last_not_of("aeiou", 20, 2); // 20
//s.find_last_not_of(str) 查找字符ch之外任意字符在當前字符串s的[0,pos]中最後一次出現的位置
int pos4 = str.find_last_not_of('r', 30); // 29