C++ 標準庫string
基礎操作
#include <string>
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
string s1 = "value_s1";
string s2(s1);
string s3("value_s3");
string s4(4,'c');
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s4 = " << s4 << endl;
cout << "size of s1:" << s1.size() << endl;
cout << "s2 is empty?:" << s2.empty() << endl;
cout << "s1 + s2 = :" << s1+s2 << endl;
string a,b;
cout << "input string:" << endl;
cin >> a >> b;
cout << "a = " << a << "b = " << b << endl;
getline從流中讀取: 讀取一整行
string c;
while (getline(cin, c)){
cout << "c = " << c << endl;
}
string str="ab!!cd,,efg";
for(auto c: str){
cout << c <<endl;
}
cout << "使用範圍for語句遍歷string:"<<endl;
decltype(str.size()) count = 0;
for(auto c:str){
if(ispunct(c))
count++;
}
for(auto &c:str){
c = toupper(c);
}
cout <<"number of puncts:"<< count << endl;
cout << "str = "<<str<<endl;
cout << "下標訪問" <<endl;
for(int i = 0; i!=str.size();i++){
cout << str[i] <<endl;
}
cout << "迭代器訪問" <<endl;
for(auto it=str.begin(); it!=str.end(); ++it){
cout<<*it<<endl;
}
return 0;
}
進階操作
構造、初始化string
string s(cp,n)
從數組中拷貝前n個字符
string s(s2,pos2)
從字符串s2的pos2位置開始拷貝
string s(s2,pos2,len2)
從字符串s2的pos2位置拷貝len2個字符
const char *cp = "hello world!!";
char p[]={'h','w'};
string s1(cp);
string s2(p);
string s3(p,2);
string s4(cp+6,5);
string s5(s1,6,5);
string s6(s1,6);
string s7(s1,6,20);
string s8(s1,16);
s.substr(pos,n)
從string的pos位置開始拷貝n個字符
string s("hello world");
string s2=s.substr(0,5);
string s3=s.substr(6);
string s4=s.substr(6,11);
string s5=s.substr(12);
更改string
- string的
insert()
和erase()
可以接受迭代器,也可以接受下標;
s.insert(s.size(),5,'!');
s.insert(0,s2);
s.insert(0,s2,0,s2.size());
s.erase(s.size()-5,5);
const char *cp="Stately,plump Buck";
s.assign(cp,7);
s.insert(s.size(),cp+7);
string s1("abcd"),s2=s1;
s1.insert(s1.size(),"efg");
s2.append("efg");
s1.replace(4,3,"EFG");
s2.replace(4,3,"EFGH");
搜索操作
s.find(args)
在s中尋找args第一次出現的位置
s.rfind(args)
在s中尋找args最後一次出現的位置
s.find_first_of(args)
在s中尋找args中任意元素第一次出現的位置
s.find_first_not_of(args)
在s中尋找args中任意元素第一次沒出現的位置
s.find_last_of(args)
在s中尋找args中任意元素最後一次出現的位置
s.find_last_not_of(args)
在s中尋找args中任意元素最後一次沒出現的位置
string s("Abcdefgsdf");
auto pos=s.find("Abcd");
auto pos1=s.rfind("f");
cout<<pos<<" "<<pos1<<endl;
string numbers("0123456789"),name("rbd35k4");
auto pos2=name.find_first_of(numbers);
auto pos3=name.find_last_of(numbers);
auto pos4=name.find_first_not_of(numbers);
auto pos5=name.find_last_not_of(numbers);
cout<<pos2<<" "<<pos3<<" "<<pos4<<" "<<pos5<<" "<<endl;
練習:查找string中的所有數字的位置
string temp("al46ien7g234lhbo4534ohln65oh4oh7j653dhg");
string::size_type pos=0;
while(temp.find_first_of(numbers,pos)!=string::npos){
pos=temp.find_first_of(numbers,pos);
cout<<"position:"<<pos<<endl;
pos++;
}
compare函數:
- 大於返回正數,小於返回負數,等於返回0
s.compare(s2)
s與整個s2比較
s.compare(pos1,n1,s2)
s從pos1開始的n1個字符與整個s2比較
s.compare(pos1,n1,s2,pos2,n2)
s從pos1開始的n1個字符與s2從pos2開始的n2個字符比較
s.compare(cp)
s與cp指向的字符數組比較
s.compare(pos1,n1,cp)
s從pos1開始的n1個字符與cp指向的字符數組比較
s.compare(pos1,n1,cp,n2)
s從pos1開始的n1個字符與cp指向的地址開始的n2個字符比較
數值轉換
- 非成員函數
to_string(val)
:將任何算數類型數值val轉換成string
stoi(s)
:string轉換成int整型
stol(s)
:string轉換成long整型
stoul(s)
:string轉換成unsigned long整型
stoll(s)
:string轉換成long long整型
stoull(s)
:string轉換成unsigned long long整型
stof(s)
:string轉換成float浮點型
stod(s)
:string轉換成double浮點型
stold(s)
:string轉換成long double浮點型
練習:提取字符串中的數值
string a("pi=3.14");
double d=stod(a.substr(a.find_first_of("+-.0123456789")))