|
楼主 |
发表于 2018-2-17 23:30:31
|
显示全部楼层
string中 find()的应用 (rfind() 类似,只是从反向查找)
原型如下:
(1)size_t find (const string& str, size_t pos = 0) const; //查找对象--string类对象
(2)size_t find (const char* s, size_t pos = 0) const; //查找对象--字符串
(3)size_t find (const char* s, size_t pos, size_t n) const; //查找对象--字符串的前n个字符
(4)size_t find (char c, size_t pos = 0) const; //查找对象--字符
结果:找到 -- 返回 第一个字符的索引
没找到--返回 string::npos
erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- using namespace std;
- int main()
- {
- char str[101];
- gets(str);
- string a=str;
- for(int i=0;i<a.size();i++)
- a[i]=tolower(a[i]);
- while(gets(str))
- {
- string b=str,c=b;
- for(int i=0;i<b.size();i++)
- b[i]=tolower(b[i]);
- int t=b.find(a,0);
- while(t!=string::npos)
- {
- c.erase(t,a.size());
- b.erase(t,a.size());
- t=b.find(a,t);
- }
- t=c.find(' ',0);
- while(t!=string::npos)
- {
- c.erase(t,1);
- t=c.find(' ',0);
- }
- cout<<c<<endl;
- }
- return 0;
- }
复制代码
|
|