string类成员函数find/find_first_of用法详解
来源:互联网 发布:linux怎么新建文件夹 编辑:程序博客网 时间:2024/05/21 13:02
一:find
函数原型:
size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;
参数说明:pos查找起始位置 n待查找字符串的前n个字符
使用样例:
string str1("the usage of find can you use it");
string str2("the");
上面定义出了两个字符串;
str1.find(str2); // 从串str1中查找时str2,返回str2中首个字符在str1中的地址
str1.find(str2,5); // 从str1的第5个字符开始查找str2
str1.find("usage"); // 如果usage在str1中查找到,返回u在str1中的位置
str1.find("o"); // 查找字符o并返回地址
str1.find("of big",2,2); // 从str1中的第二个字符开始查找of big的前两个字符
二:find_first_of
函数原型:
size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;
参数和find基本相同,不在赘述!
特别注意:
find_first_of 函数最容易出错的地方是和find函数搞混。它最大的区别就是如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功,而find则不同;
比如:
string str1("I am change");
string str2("about");
int k=str1.find_first_of(str2); //k返回的值是about这5个字符中任何一个首次在str1中出现的位置;
//the usage of find /find_first_of by heat_nan from ZZULI#include<iostream>#include<string>using namespace std;int main(){string str1("Hi,every one! I am heat_nan from ZZULI. one");string str2("heat_nan");int k=str1.find(str2);cout<<"The position of 'heat_nan' is "<<k<<endl;int k1=str1.find("one");cout<<"The postion of the first 'one' is "<<k1<<endl;int k2=str1.find("one of",k1+1,3);cout<<"The postion of the second 'one' is "<<k2<<endl;int k3=str1.find_first_of("aeiou");//here k3=1while(k3!=string::npos) //hint: here "string::npos"means find failed{str1[k3]='*';k3=str1.find_first_of("aeiou",k3+1);}cout<<str1<<endl;return 0;}
- string类成员函数find/find_first_of用法详解
- string 类成员函数 find() / find_first_of() / find_last_of() 的用法详解
- string中find和find_first_of函数用法总结
- string类中find(),find_first_of()和find_first_not_of()函数
- string 中find_first_of()函数用法例题
- C++ string类:find()和find_first_of()
- string 中的 find()、 find_first_of()、find_first_not_of()
- string 搜索功能 find find_first_of
- string类成员函数详解
- string.find()函数用法
- string c++详解 find_first_not_of() find_first_of()
- C++string中的查找函数find与find_first_of函数的区别
- string中的find(),find_first_of(),find_first_not_of()之间区别
- string的find和find_first_of的区别
- string类型中find和find_first_of
- string的find和find_first_of的区别
- find函数用法详解
- string find的用法详解
- java代码最佳实践
- linux 的date命令及系统时间设置
- 软件工程 ,UML ,设计模式 简单关系
- 认识java、Java环境的搭建与规范化编码
- 内部类详解
- string类成员函数find/find_first_of用法详解
- XCode下的iOS单元测试
- MyEclise中TCP/IP Monitor的使用
- EJB3.0学习笔记--SOAP-AXIS--深入Soap引擎
- 单例模式的七种写法
- xp与win7公用一个MyEclipse
- VC 项目中可以使用的路径宏
- Android 按power键唤醒屏幕流程
- Codefoces 13B Letter A (几何)