string 查找字符并删除_temp_two_means

//

01 string 内置 s.find()+s.erase() 函数

    01 size_t s.find( value );

        01 找到 return 下标索引
        02 未找到 return string::npos

            decv++: 
                string::npos == 18446744073709551615
                %d (18446744073709551615) == -1

        03 判断是否存在 
            value s.find( value ) != string::npos 说明存在, == 则说明不存在 
    
    02 string& s.erase( size_t pos=0,size_t n=s.size()-pos );

        01 return string& 类型 // 即 return 执行对应删除操作后 s 的引用 
        02 从下标 pos 开始,删除 n 个元素
        03 参数 n 缺省时,删除 从下标 pos 开始的所有元素
        04 两个参数都缺省时( 或 pos==0 ) 清空元素

    03 eg. // 因为内置 s.find() return 下标索引 所以无法使用 s.erase( iterator it );

        pos=s.find( ch );
        if( pos!=string::npos ) s.erase( pos,1 );

02 头文件 #include<algorithm> find() + C.erase() 函数 // 对应头文件为 #include<C>

    01 iterator find( c.begin(),c.end(),value );

    02 iterator C.erase( iterator it );
 
        01 return 被删元素的 下一个元素的 迭代器
        02 删除迭代器 it 指向的元素

    03 eg.

        it=find( s.begin(),s.end(),ch );
        if( it!=s.end() ) s.erase( it );

!!!

    01 以上两种方式都会导致 s.size() 改变, 可按照需求记录下 len=s.size();

// eg-01
#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    char ch;
    int pos,len,i;
    
    while( getline( cin,s ) )
    {
        cin.get(ch); cin.get();
        len=s.size();

        for( i=0;i<len;i++ )
        {
            pos=s.find( ch );
            if( pos!=string::npos ) 
			{
				string& it=s.erase( pos,1 );
				cout<<it<<endl;
			}
            cout<<s.size()<<endl;
        }
        cout<<s<<endl;
    }
    return 0;
}

// eg-02
#include<bits/stdc++.h>
using namespace std;

string::iterator it;

int main()
{
	string s;
	char ch;
    int len,i;
	
    while( getline( cin,s ) )
    {
        cin.get(ch); cin.get();
        len=s.size();

        for( i=0;i<len;i++ )
        {
            it=find( s.begin(),s.end(),ch );
            if( it!=s.end() )
			{
				it=s.erase(it);
            	if( it!=s.end() ) 	cout<<*it<<endl;
            	else				cout<<"end!"<<endl;
			}
            cout<<s.size()<<endl;
        }
        cout<<s<<endl;
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这题AC再睡.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值