STL-string

目录

访问:

下标

迭代器.

操作:加

+:"abc"+"d"="abcd"

比较大小(字典序)==<=!=

字符串长度:.length()或者。size(一样)

插入insert(***):

 str.insert(num,str2)

str2.insert(it,it1,it2)

​编辑 ​编辑

删除erase()

erase(迭代器)【点删除】

erase(迭代器,迭代器) 【区间删除】左闭右开

erase(数字1,数字2)

清空clear()

取子串substr(pos,len)

寻找子串是否存在.find()

替换replace(pos,len,str)


访问:

下标

但是中文会bug

迭代器.

迭代器遍历

zhonwenbug: 

操作:加

+:"abc"+"d"="abcd"

比较大小(字典序)==<=!=

字符串长度:.length()或者。size(一样)

插入insert(***):

 str.insert(num,str2)

在str的位置为num插入str

str2.insert(it,it1,it2)

在it(迭代器)位置插入【左闭右开】it1-it2(迭代器)的字符串

 

删除erase()

erase(迭代器)【点删除】

erase(迭代器,迭代器) 【区间删除】左闭右开

erase(数字1,数字2)

删除1处向后2长度的字符串

清空clear()

取子串substr(pos,len)

返回值:从pos位置起,长度为len的子串

寻找子串是否存在.find()

str.find(str2)

str.find(str2,pos):pos(index数字)开始,是否存在

返回值:

存在:返回位置坐标

不存在:返回string::npos

替换replace(pos,len,str)

把pos位置(号位index),长度为len的子串删除,然后在pos位置插入str。

也可以使用迭代器代替pos&len:

replace(it1,it2,str),表示从it1到it2之间的删除。

∴len不一定等于str长度

【存档()】

#include <cstdio>
#include <queue>
#include <vector> 
#include <set> 
#include <string>
#include <iostream>
#include <map>
using namespace std;

const int MAXN = 100000;
set<int> st;
string str1,str2;
map<char,int> mp;
int main()
{
mp['#']=3;
mp['a']=4;
mp['A']=5;
	
str1="yhl! hello hell";str2="c4bomb";
str1.replace(5,5,str2); 
//str1.replace(str1.begin() ,str1.end()-1 ,str2);
for(int i=0; i<str1.size() ;i++)
cout<<i<<":"<<str1[i]<<"\n";
cout<<str1.find("hell",6)<<"是否存在";
//str1.insert(2,str2);
//cout<<(string::npos)<<"#";
str1.erase(1,2);
cout<<str1.length();
for(string::iterator its=str1.begin();its!=str1.end();its++)
cout<<*its<<"()";


set<int>::iterator it;
it=st.begin();
for(it=st.begin() ;it!=st.end();it++)
printf("%d ",*it);
printf("size:%d\n",st.size() );
it=st.find(1);
set<int>::iterator it2;
it2=st.find(3);
it2++;
//st.erase(4); 
st.erase(it,it2);
for(it=st.begin() ;it!=st.end();it++)
printf("%d ",*it);

 

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值