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