string find()函数、string::npos的含义、erase()函数

叮叮加粗样式~~
一、string find()函数
第一种,algorithm头文件的find()。

使用方法:find(begin,end,value)
解释:第一个参数是容器或者数组的起始地址(容器.begin()或者数组名),也可以是任意地址,不非法即可;第二个参数是结束查找的地址(容器.end()或者数组名+长度),value是想要查找的字符或者字符串。查找成功将返回迭代器(容器)或者指针(数组),否则返回end()。

第二种,string自带的find()。
使用方法:1.例如在string1中查找string2,string1.find(string2);将返回string2第一次在string1中出现的位置。
如果希望在特定位置开始查找,使用 string1.find(string2,location);

#include <iostream>
#include<string>
using namespace std;
int main()
{
    string str,sub;
    cin>>str;
    cin>>sub;
    int pos=str.find(sub);//从0开始找!!!
    if(pos!=string::npos)//npos是一个常数,用来表示不存在的位置
        cout<<"sub串在str中的位置是"<<pos<<endl;
    else
        cout<<"sub在str中串未出现"<<endl;
}

二、string::npos的含义
string::npos参数 —— npos 是一个常数,用来表示不存在的位置

例如,有两个字符串a、b,判断a字符串是否包含b字符串

//如果字符串不存在包含关系,那么返回值就一定是npos
if(a.find(b)!=string::npos){
	cout<<"yes"<<endl;
}else{
	cout<<"no"<<endl;
}

string::size_type pos;

str.find("字符串") 返回值是字符在母串s中的下标位置;

str.find("字符串",9)从s的下标9开始,查找字符串,返回字符串在s中的下标;

pos=s.find(str,pos)查找s中str出现的所有位置`

pos=s.find_first_of(str) 返回str出现在母串s中的首次出现的位置

pos=s.find_last_of(str)返回str出现在母串s中的最后一次出现的位置

#include <iostream>
#include<string>
using namespace std;
int main()
{
    string s("abcbcdcd");
    string str="cd";
    string::size_type pos;
    pos=s.find("bc");
    if(pos!=s.npos){
        cout<<pos<<endl;
    }
    else{
      cout<<"no"<<endl;
    }
    pos=s.find("bc",2);
    cout<<"从s下标2开始查找"<<pos<<endl;
    int i=1;
    while((pos=s.find(str,pos))!=string::npos){
        cout<<"位置"<<i<<":"<<pos<<endl;
        pos++;
        i++;
    }
    cout<<"第一次:"<<s.find_first_of('d')<<endl;
    cout<<"最后一次:"<<s.find_last_of('d')<<endl;

}


在这里插入图片描述
三、C++ STL中string::substr(size_type __pos, size_type __n)解析
string::substr(pos, n)表示截取字符串string,从pos下标开始,长度为n的子串。
比如:str = “helloworld”,那么str.substr(2, 3)等于“llo”。
1分析substr()源码
substr()源码如下:

 basic_string substr(size_type __pos = 0, size_type __n = npos) const {
    if (__pos > size())
      _M_throw_out_of_range();
    return basic_string(_M_start + __pos, 
                        _M_start + __pos + min(__n, size() - __pos));
  }

即如果__n的默认缺省等于npos的话,就截取从__pos到末尾的全部字符串

四、erase()函数
(1)erase(pos,n);删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)

#include <iostream>
#include<string>
using namespace std;
int main()
{
    string s("I love zufe.");
    string ::iterator it;
    s.erase(1,5);
    cout<<s<<endl;
    it=s.begin()+6;
    s.erase(it);
    cout<<s<<endl;
    s.erase(s.begin()+3,s.end()-2);
    cout<<s<<endl;
}


在这里插入图片描述五、C++ STL string迭代器的使用

#define  _SCL_SECURE_NO_WARNINGS
 
#include <iostream>
#include <string>	//要与c语言里面的#include <string.h>区分
using namespace std;
 
int main(){
 
	string str("abcdefg");
	string::iterator ite;
 
	ite = str.begin();
	for (size_t i = 0; i < str.size(); i++){
		cout << *ite;
		ite++;
	}
	ite = str.begin();
	cout << endl;
	for (size_t i = 0; i < str.size(); i++){
		cout << ite[i];
	}
	cout << endl;
	ite = str.begin();
	for (; ite != str.end(); ite++){
		cout << *ite;
	}
 
	cout << endl;
 
	str.append(10, 'a');
	//cout << ite[16] << endl; 迭代器失效
 
	system("pause");
	return 0;
}
  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值