1.输入输出
str.lenght()/str.size() 两个函数都可以取得string的长度,元素个数
直接cin cout输入输出
非要用printf()输出得str.c_str() 转换成c语言中的字符数组
#include<iostream>
#include <cstdio>
#include<string>
using namespace std;
int main(){
string str;
cin>>str;
printf("%s\n", str.c_str());//c_str()转换为字符数组
return 0;
}
2.迭代器访问
有些api必须iterator类型参数
注意string::iterator it;//没有<>
#include<iostream>
#include<string>
using namespace std;
int main(){
string str="abcdef";
string::iterator it;
for(it=str.begin();it!=str.end();it++){
printf("%c", *it);
}
return 0;
}
3.+ > < == <= >= !=
可以直接用
4.insert
insert(pos,string) 在pos处(pos下标左边)插入字符串string
#include<iostream>
#include<string>
using namespace std;
int main(){
string str1="abcxyz",str2="123";
str1.insert(3,str2);
cout<<str1<<endl;
return 0;
}
insert(it,it1,it2)
在it处(it迭代器所指位置左边)插入字符串[it1,it2)区间内的字符串
#include<iostream>
#include<string>
using namespace std;
int main(){
string str1="abcxyz",str2="123";
str1.insert(str1.begin()+3,str2.begin()+1,str2.end());
cout<<str1<<endl;
return 0;
}
5.erase删除
1.删除单个元素
str.erase(it) 删除迭代器it所指位置的元素
2.删除区间元素
str.erase(first,end); 删除迭代器区间[first,end)的所有元素
str.erase(index,length); 删除index下标开始,后面数共length个字符 ★★
#include<iostream>
#include<string>
using namespace std;
int main(){
string str="hello2world";
cout<<"原始:"<<str<<endl;
str.erase(str.begin()+5);
cout<<"删除下标5后:"<<str<<endl;
str.erase(str.begin(),str.begin()+5);
cout<<str<<endl;
str.erase(1,3);
cout<<"下标1开始往后删除3个:"<<str<<endl;
return 0;
}
6.clear()清空string中的数据,O(1)
7.substr()
substr(pos,len)返回pos下标开始,长度为length的子串
#include<iostream>
#include<string>
using namespace std;
int main(){
string str="Thank you for your smile.";
cout<<str.substr(0,5)<<endl;
cout<<str.substr(14,4)<<endl;
cout<<str.substr(19,5)<<endl;
return 0;
}
8.string::npos
string::npos unsigned_int类型的-1,作为find函数失败时的返回值,即没有找到
值为-1或者4294967295=2^32-1 unsigned类型最大值
#include<iostream>
#include <string>
using namespace std;
int main(){
cout<<(string::npos==-1)<<endl;
cout<<(string::npos==4294967295)<<endl;
return 0;
}
都返回true
9.find
str.find(str2) str2是str子串时返回第一次出现的位置(下标),未找到返回string::npos
str.find(str2,n) 从下标n处开始找,返回值同上
#include<iostream>
#include <string>
using namespace std;
int main(){
string str="Thank you for your smile";
string str2="you";
string str3="me";
if(str.find(str2)!=string::npos){
cout<<str.find(str2)<<endl;
}
if(str.find(str2,7)!=string::npos){
cout<<str.find(str2,7)<<endl;
}
if(str.find(str3)!=string::npos){
cout<<"找到\n";
}else{
cout<<"未找到\n";
}
return 0;
}
10.replace()
#include<iostream>
#include<string>
using namespace std;
int main(){
string str="Maybe you will turn around.";
string str2="will not";
string str3="surely";
cout<<str.replace(10,4,str2)<<endl;//will换成will not
cout<<str.replace(str.begin(),str.begin()+5,str3)<<endl;//Maybe换成surely
cout<<str<<endl;//此API影响str本身
return 0;
}
补充:
高级查找:
str.find_first_of("xyz"); 返回字符串str中第一个和"xyz"字符串中任意一个字符匹配的字符的下标
str.find_first_not_of("xyz"); 返回字符串str中第一个和"xyz"字符串中任意一个字符都不匹配的字符的下标
作用一样,从后往前找
str.find_last_of("xyz");
str.find_last_not_of("xyz");
#include<iostream>
#include<string>
using namespace std;
int main(){
string s="123abc";
cout<<s.find_first_of("efga")<<endl;
cout<<s.find_first_not_of("321")<<endl;
cout<<s.find_first_not_of("321a")<<endl;
return 0;
}