0
点赞
收藏
分享

微信扫一扫

算法笔记6.3 string


1.输入输出

str.lengh()/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;
}

算法笔记6.3 string_sting

 

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;
}

算法笔记6.3 string_字符串_02

 

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;
}

算法笔记6.3 string_ios_03

 

 

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;
}

算法笔记6.3 string_ios_04

 

 

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.3 string_STL_05

 

6.clear()清空string中的数据,O(1)

 

7.substr()

substr(pos,len)返回pos下标开始,长度为length的子串

substr(pos) 返回pos下标开始(包含pos)后面所有元素

#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;
cout<<str.substr(5);//5下标开始 截取后面所有元素
return 0;
}

算法笔记6.3 string_ios_06

 

 

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

算法笔记6.3 string_STL_07

 

 

9.find

str.find(str2) str2是str子串时返回第一次出现的位置(下标),未找到返回string::npos
str.find(str2,n) 从下标n处开始找,返回值同上

str2可以是字符串 也可以是单个字符

#include<iostream>
#include <string>
using namespace std;
int main(){
string str="Thank you for your smile";
string str2="you";string str3="me";
char c1='a',c2='b';

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";
}

//字符也行
cout<<"c1:"<<str.find(c1)<<endl;
cout<<"c2:"<<(int)str.find(c2)<<endl;

return 0;
}

算法笔记6.3 string_ios_08

 

 

10.replace()

replace(s,len,str);  下标为s开始,后面len个字符替换成str

#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;
}

算法笔记6.3 string_STL_09

 

 

 

补充:

高级查找:

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;
}

算法笔记6.3 string_STL_10

 

string转char   string.c_str()

char转string 直接赋值  String=str

string从中间某位开始输出: 先转char数组再指针加   比如从下标3开始往后输出  cout<<string.c_str()+3;

 

 

举报

相关推荐

0 条评论