C++標準庫string學習筆記
string概述
作為c++語言用作字符串處理的標準庫,和Python字符串類型有較多相似之處。可以使用‘=’進行賦值,“==”來比較,“+”進行拼接。
構造函數
- string s;直接聲明變量
- string s(str);創建一個str的副本s
- string s(str,idx);將str字符串索引值及其后面的部分當做s初值
- string s(str,idx,len);將str從idx開始,長度為len的內容賦值給s
- string s(c-str);將char*字符串作為字符串s初值
- string s(chars,len);以字符數組的前len個元素作為s初值
- string s(num,c);生成num個c字符的字符串。c為int類型
- string s(begin,end);將[begin, end]區間內的字符做為s初值,begin,end為可迭代對象
- string s(init-list);將init-list內字符做為s初值
示例代碼
點擊查看代碼
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
/*構造字符串*/
string str1="hello world";
cout<<"str1:"<<str1<<endl;
string str2(str1);
cout<<"str2:"<<str2<<endl;
string str3(str1,2);
cout<<"str3:"<<str3<<endl;
string str4(str1,2,3);
cout<<"str4:"<<str4<<endl;
char word[20]="new string";
string str5(word,5);
cout<<"str5:"<<str5<<endl;
string str6(word);
cout<<"str6:"<<str6<<endl;
string str7(&str6[2],&str6[7]);
cout<<"str7:"<<str7<<endl;
char *a="c-----string";
string str8(a,5);
cout<<"str8:"<<str8<<endl;
string str9(a);
cout<<"str9:"<<str9<<endl;
string str10(10,121);
cout<<"str10:"<<str10<<endl;
return 0;
}
輸出結果為:
----------------------------------------
str1:hello world
str2:hello world
str3:llo world
str4:llo
str5:new s
str6:new string
str7:w str
str8:c----
str9:c-----string
str10:yyyyyyyyyy
進程已結束,退出代碼為 0
--------------------------------------------------
string值操作
- =,assign(str);直接賦值,相互等價
- swap(str1,str2);將str1與str2值交換
- +,append(str);字符串拼接
- push_back(c);c為int類型
- pop_back();移除末尾字符
- insert(pos,str);指定pos處添加str
- erase(i);保留前i位字符,i<=str.size()
- resize(len);保留前len位字符,長度不夠則補位;
示例代碼
點擊查看代碼
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
string str1="hello world";
cout<<"str1:"<<str1<<endl;
/*賦值操作*/
str1="你好";
cout<<"str1:"<<str1<<endl;
str1.assign("a new string!");
cout<<"str1:"<<str1<<endl;
string str2;
str2.assign("1234567");
cout<<"str2:"<<str2<<endl;
swap(str1,str2);
cout<<"str1:"<<str1<<endl;
cout<<"str2:"<<str2<<endl;
str2.append(",ok");
cout<<"str2:"<<str2<<endl;
str1+="10";
cout<<"str1:"<<str1<<endl;
str1.push_back('1');
cout<<"str1:"<<str1<<endl;
str1.pop_back();
cout<<"str1:"<<str1<<endl;
str2.insert(3,"insert data");
cout<<"str2:"<<str2<<endl;
str2.erase(22);
cout<<"str2:"<<str2<<endl;
cout<<"str2大小:"<<str2.size()<<endl;
str1.resize(16);
cout<<"str1:"<<str1<<endl;
}
輸出結果為:
----------------------------------------
str1:hello world
str1:你好
str1:a new string!
str2:1234567
str1:1234567
str2:a new string!
str2:a new string!,ok
str1:123456710
str1:1234567101
str1:123456710
str2:a ninsert dataew string!,ok
str2:a ninsert dataew strin
str2大小:22
str1:123456710
進程已結束,退出代碼為 0
--------------------------------------------------
替換字符str.replace()
- str.replace(pos,n,str2);將str 從[pos]開始的n個元素用str2替代;
- str.replace(pos1,n1,str2,pos2,n2);將str 從[pos1]開始的n1個元素用str2從[pos2]開始的n2個元素替代;
- str.replace(i1,i2,k1,k2);將i1-i2元素用k1-k2元素代替,i1,i2,k1,k2均為可迭代對象
點擊查看代碼
#include <iostream>
#include <string>
using namespace std;
int main() {
string str3 = "0123456780000";
string str4 = "asdfghjkl@#$";
cout << "str3:" << str3 << endl;
cout << "str4:" << str4 << endl;
str4.replace(5, 5, str3);
cout << "str4:" << str4 << endl;
str4.replace(0, 7, str3, str3.find("2"), 4);
cout << "str4:" << str4 << endl;
str4.replace(str4.begin() + 3, str4.begin() + 5, str3.begin(), str3.end() - 3);
cout << "str4:" << str4 << endl;
}
輸出結果:
--------------------------------------
str3:0123456780000
str4:asdfghjkl@#$
str4:asdfg0123456780000#$
str4:234523456780000#$
str4:23401234567803456780000#$
進程已結束,退出代碼為 0
---------------------------------------
字符串與數字相互轉換
字符串轉數字
- stoi();轉換為int型
- stol();轉換為long int型
- stoll();轉換為long long 型
- stoul();轉換為 unsigned long型
- stoull();轉換為unsigned long long 型
- stof();轉換為float型
- stod();轉換為double型
- stold();轉換為long double型
- to_string();將數字轉換為string;
點擊查看代碼
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "-12345";
string str2 = "-345.78888";
auto num1 = stoi(str1);
auto num2 = stol(str1);
auto num3 = stold(str1);
cout << "num1=" << num1 << endl;
cout << "num2=" << num2 << endl;
cout << "num3=" << num3 << endl;
string str3 = "12345";
auto num4 = stoul(str3);
auto num5 = stoull(str3);
cout << "num4=" << num4 << endl;
cout << "num5=" << num5 << endl;
auto num6 = stof(str2);
auto num7 = stod(str2);
auto num8 = stold(str2);
cout << "num6=" << num6 << endl;
cout << "num7=" << num7 << endl;
cout << "num8=" << num8 << endl;
auto num10=-12.3456;
auto str4= to_string(num10);
cout << "num10->str:" << str4 << endl;
}
輸出結果
----------------------------------
num1=-12345
num2=-12345
num3=-12345
num4=12345
num5=12345
num6=-345.789
num7=-345.789
num8=-345.789
num10->str:-12.345600
進程已結束,退出代碼為 0
------------------------------------------------
***不可將“-123"轉換為unsigned 型,不可將“123.12”轉換為int型,可以將“-1234”轉換為浮點類型***
字符搜索和查找
- find()
- rfind()
- find_first_of()
- find_last_of()
- find_first_not_of()
- find_last_not_of()
實參(value,pos,n):
value:要查找的字符值
pos,開始查找的位置
n,查找范圍
返回值,索引
示例代碼
點擊查看代碼
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "In addition, some swarm intelligence methods that mimic\n "
"biological behaviors are also gradually applied to regional\n"
"coverage planning tasks. Reference proposed a rule-\n"
"based collaborative search algorithm based on the kinetic\n"
"model-Boids.";
cout<<str1.size()<<endl;
string str2(str1,100,10);
cout<<str2<<endl;
cout<<str1.find_first_of("t",100,100);
}
輸出結果:
-------------------------------------
244
lied to re
104
進程已結束,退出代碼為 0
-----------------------------------------------

浙公網安備 33010602011771號