本文转载自http://blog.csdn.net/youxin2012/article/details/9162415

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
 
示例:
 #include <iostream>       // std::cout
#include <string> // std::string int main ()
{
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle"); // different member versions of find in the same order as above:
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n'; found=str.find("needles are small",found+,);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n'; found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n'; found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n'; // let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition"); //replace 用法
std::cout << str << '\n'; return ;
}
结果:
first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles
 
 
其他还有  find_first_of(), find_last_of(), find_first_not_of(), find_last_not_of()
作用是查找   字符串中 任一个字符 满足的查找条件
string snake1("cobra");
int where = snake1.find_first_of("hark");
返回3  因为 "hark"中 各一个字符 在 snake1--cobra 中第一次出现的是  字符'r'(3为 cobra 中'r'的索引)
同理:
int where = snake1.find_last_of("hark");
返回4  因为 "hark"中 各一个字符 在 snake1--cobra 中最后一次出现的是  字符'a'(3为 cobra 中'r'的索引)
 
其他同理
 
 

C++中find()函数和rfind()函数的用法的更多相关文章

  1. C++中string类的操作函数。

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...

  2. Java实现Excel中的NORMSDIST函数和NORMSINV函数

    由于工作中需要将Excel中的此两种函数转换成java函数,从而计算内部评级的资本占用率和资本占用金额.经过多方查阅资料和整理,总结出如下两个转换方法 标准正态分布累计函数NORMSDIST: pub ...

  3. javascript中需要自行定义的函数

    1.toString()与String() 使用方式:x.toString();-----String(x); String是万能的方法,而x.toString()则有局限性,无法将undefined ...

  4. 跟着百度学PHP[5]函数篇2-PHP中的特殊形式的函数

    目录...................................................... .00x1 可变函数 在PHP里面如果说将“函数名称”赋予字符串类型的变量.在调用这个 ...

  5. Oracle中的CHR()函数与ASCII()函数

    工作中经常会处理一些因特殊字符而导致的错误,如上周我就遇到了因为换行符和回车符导致的数据上报的错误,这种错误比较难以发现,通常是由于用户的输入习惯导致的,有可能数据极少,就那么几行错误从而导致整个数据 ...

  6. angular中的compile和link函数

    angular中的compile和link函数 前言 这篇文章,我们将通过一个实例来了解 Angular 的 directives (指令)是如何处理的.Angular 是如何在 HTML 中找到这些 ...

  7. javascript中函数声明和函数表达式浅析

    记得在面试腾讯实习生的时候,面试官问了我这样一道问题. //下述两种声明方式有什么不同 function foo(){}; var bar = function foo(){}; 当初只知道两种声明方 ...

  8. PHP中的回调函数和匿名函数

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  9. 总结javascript中的全局变量和局部变量的区别以及声明函数和调用函数的区别

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...

随机推荐

  1. mysql 权限管理 针对某个库 某张表 授权 tables_priv表

    精确到表级别 针对db1的t3表 授予select权限 mysql> grant select on db1.t3 to 'mike'@'localhost'; Query OK, rows a ...

  2. [py]一些搜集到的问题

    过滤爬虫爬取下来的关键字 v1,来不及了,先上车 content = ['哈士奇', '二哈', '哈士奇图片','哈士奇图片', '哈士奇美丽价格', '哈士奇是个大傻逼', '猫咪图片', '猫咪 ...

  3. [参考资料] 80个Python经典资料(教程+源码+工具)汇总

    AD : 2018重磅地面课程<机器读心术之语音识别前沿实战特训营>,迈向人工智能新高度 [专题推荐]Python系列英文原版电子书 http://down.51cto.com/zt/10 ...

  4. linux命令:linux权限管理命令

    权限管理命令   文件的权限只有你两个人可以更改,一个是root,一个是文件所有者. 命令名称:chmod 命令英文原意:change the permissions mode of a  file ...

  5. Qt QDataTime QString 两个类的使用

    QDateTime now = QDateTime::currentDateTime(); QString nowStr; nowStr = now.toString("yyyyMMdd_h ...

  6. Linux系统——MySQL基础(一)

    # 数据库 ## 数据库简单的分类:(1)关系型数据库:MySQL和Oracle.Postgresql(2)非关系型数据库:Memcached和Redis(3)消息队列中间件(4)搜索引擎数据库:El ...

  7. input hidden的作用

    网上辣么多blabla的,我就总结一下 1 什么是hidden hidden 是input type的一个值,叫隐藏域,官方解释:“Hidden 对象代表一个 HTML 表单中的某个隐藏输入域. 这种 ...

  8. Codeforces Round #246 (Div. 2) D E

    这题说的是给了一个字符串当前缀和后缀相同的时候就计算此时的 整个串种拥有这样的子串友多少个,没想到用KMP解 用0开头的那种类型的 KMP 今天刚好也学了一下,因为KMP的作用是找出最长前缀 KMP ...

  9. WebStorm使用快速入门

    WebStorm建立在开源IntelliJ平台之上,JetBrains已经开发和完善了超过15年.其提供了统一的UI,可与许多流行的版本控制系统配合使用,确保在git,GitHub,SVN,Mercu ...

  10. 主成分分析(PCA)学习笔记

    这两天学习了吴恩达老师机器学习中的主成分分析法(Principal Component Analysis, PCA),PCA是一种常用的降维方法.这里对PCA算法做一个小笔记,并利用python完成对 ...