string的使用
c語言中一般使用字符數組來存放字符串,但是這樣使用起來容易出現一些錯誤。為了避免使用時候的麻煩和錯誤,c++在stl中加string類,對常用的功能進行了封裝。
string的定義
1.頭文件包含string
# include <string>
# include <iostream>
using namespace std;
2.定義和初始化
string str;
string str="abcd";
string內容訪問
一般來說可以通過字符串數組方式訪問string
通過下標訪問
# include <iostream>
using namespace std;
int main(void)
{
string str="abcd";
for(int i=0;i<str.length();i++)
{
cout<<str[i]<<' '<<endl;
}
return 0;
}
輸入輸出整個字符串
# include <iostream>
using namespace std;
int main(void)
{
string str;
cin>>str;
cout<<str<<endl;
return 0;
}
通過迭代器訪問
# include <iostream>
using namespace std;
int main(void)
{
string str="abcde";
for(string::iterator it=str.begin();it!=str.end();it++)
{
cout<<*it<<endl;
}
return 0;
}
string常見函數
字符串拼接
# include <iostream>
using namespace std;
int main(void)
{
string str1="abc";
string str2="def";
string str3;
str3=str1+str2;
str2+=str1;
cout<<str1<<' '<<str2<<' '<<str3<<endl;
return 0;
}
字符串比較
按照字典的順序來比較字符串
# include <iostream>
using namespace std;
int main(void)
{
string str1="abc";
string str2="def";
if(str1>str2)
cout<<"str1>str2"<<endl;
else
cout<<"str1<str2"<<endl;
return 0;
}
獲取字符串長度
size()和length()函數都是獲取當前字符串的長度,區別不大
# include <iostream>
using namespace std;
int main(void)
{
string str1="abc";
string str2="def";
cout<<str1.length()<<' '<<str2.size()<<endl;
return 0;
}
插入字符串
insert(pos,string)
# include <iostream>
using namespace std;
int main(void)
{
string str1="Hello";
string str2="world";
str1.insert(2,str2);
cout<<str1<<endl;
//outcome=Heworldllo
return 0;
}
insert(it,it2,it3)
it是要插入的位置
it2,it3是要插入字符的首尾迭代器
# include <iostream>
using namespace std;
int main(void)
{
string str1="Hello";
string str2="world";
str.insert(str1.begin()+2,str2.begin(),str2.end());
//outcome=Heworldllo
return 0;
}
刪除單個元素或者區間元素
刪除單個元素
# include <iostream>
using namespace std;
int main(void)
{
string str="abcdef";
str.erase(str.begin()+2);//刪除第三個元素
cout<<str<<endl;//abdef
return 0;
}
刪除區間元素
erase(first,lase),first為要刪除區間的首迭代器,end是要刪除區間的尾部迭代器
# include <iostream>
using namespace std;
int main(void)
{
string str="abcdef";
str.erase(str.begin(),str.begin()+2);
cout<<str<<endl;//[str.begin(),str.begin()+2)區間的元素
return 0;
}
erase(pos,length),pos刪除開始的位置,而lenth是要刪除的長度
# include <iostream>
using namespace std;
int main(void)
{
string str="abcdef";
str.erase(2,2);
cout<<str<<endl;//"abef"
return 0;
}
刪除所有數據
# include <iostream>
using namespace std;
int main(void)
{
string str="abcdef";
str.clear();
cout<<str.length()<<endl;//0
return 0;
}
字符串切片substr
# include <iostream>
using namespace std;
int main(void)
{
string str="abcdef";
str.substr(0,4);//[0,4)
cout<<str<<endl;//abcd
return 0;
}
string::npos
string::npos是一個常數,其本身值為-1,但是由於是unsigned_int類型,因此實際上也可以認為是unsigned_int的最大值,string::npos是find函數失配的返回值
字符串匹配find
str.find(str2),str2是str的子字符串,返回str第一次出現str2的位置
str.find(str2,pos),str從pos位開始匹配str2,返回第一次出現位置的下標
# include <iostream>
using namespace std;
int main(void)
{
string str="Thank you for your smile";
string str2="you";
string str3="me";
if(str.find(str2)!=str::npos)
cout<<str.find(str2);
if(str.find(str2,7)!=str::npos)
cout<<str.find(str2,7);
if(str.find(str3)!=str::npos)
cout<<str.find(str3);
return 0;
}
字符串替換
str.replace(pos,len,str2),str從pos位置起,長度為len的范圍,全部替換為str2
str.replace(it1,it2,str2),str迭代器[it1,it2)范圍替換為str2
# include <iostream>
using namespace std;
int main(void)
{
string str1="Hello World! I am coming!";
string str2="me";
string str3="you";
str1.replace(0,2,str2);
str1.replace(str1.begin(),str1.end(),str3);
cout<<str1<<endl;
str1.replace(0)
return 0;
}