本文转载自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. C语言概述

    打印摄氏度 /* 1.1 使用int类型进行计算 */ #include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, ...

  2. Loadrnner 参数化策略

    参数化策略 关键:类型+数据+策略 1.Select next row ( 如何取) 选择下一行 1)Sequential:顺序的 每个VU都从第一行开始,顺序依次向下取值:数据可以循环重复使用:-- ...

  3. 下厨房6月26日数据丢失事故总结 MYSQL主分区被rm 命令误删除

    下厨房6月26日数据丢失事故总结 MYSQL主分区被rm 命令误删除 http://tech.xiachufang.com/?p=18 在6月26日凌晨12点左右,我们在做线上数据库的备库时,误将线上 ...

  4. HTML5插件

    HTML 助手(插件) 辅助应用程序(helper application)是可由浏览器启动的程序.辅助应用程序也称为插件. 辅助程序可用于播放音频和视频(以及其他).辅助程序是使用 <obje ...

  5. Scala系统学习(二):Scala开发环境安装配置

    Scala可以安装在任何基于UNIX/Linux或基于Windows的系统上.在您的机器上开始安装Scala之前,必须在计算机上安装Java 1.8或更高版本. 下面请按照以下步骤安装Scala. 步 ...

  6. Spark Core(四)用LogQuery的例子来说明Executor是如何运算RDD的算子(转载)

    1. 究竟是怎么运行的? 很多的博客里大量的讲了什么是RDD, Dependency, Shuffle.......但是究竟那些Executor是怎么运行你提交的代码段的? 下面是一个日志分析的例子, ...

  7. mybatis中使用where in查询时的注意事项

    我使用的时候collection值为mapper的参数名如:int deleteRoleByUserIds(@Param("userIds") String[] userIds); ...

  8. web.xml中对post请求的乱码问题解决

    直接在web.xml中添加如下代码: <filter> <filter-name>encodingFilter</filter-name> <filter-c ...

  9. [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  10. 数据分析与挖掘 - R语言:K-means聚类算法

    一个简单的例子!环境:CentOS6.5Hadoop集群.Hive.R.RHive,具体安装及调试方法见博客内文档. 1.分析题目--有一个用户点击数据样本(husercollect)--按用户访问的 ...