本文转载自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. Python实现常用的逻辑数据结构

    逻辑数据结构包括:线形结构.树形结构.图形结构.集合:存储结构包括:顺序存储.链式存储.索引存储.散列存储. 同一种逻辑结构可以有四种存储结构,不同的存储结构增.删.查.改的速度不同.逻辑结构与存储结 ...

  2. [LeetCode] 285. Inorder Successor in BST_Medium tag: Inorder Traversal

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...

  3. sqlplus与shell互相传值的几种情况

    2578人阅读   sqlplus与shell互相传值的几种情况 情况一:在shell中最简单的调用sqlplus $cat test.sh #!/bin/sh sqlplus oracle/orac ...

  4. eigen 笔记1

    c++ 的 eigen 类似于 python 的 numpy, 还有一个类似的库是 Armadillo, 当然还有 opencv. Armadillo 与 matlab 在函数名称上更接近, 但是 T ...

  5. jmeter 基础功能详解

    jmeter 基础功能详解 thread group:包含一组线程,每个线程独立地执行测试计划. sampler:采样器,有多种不同的sample实现,用来发起各种请求,如http请求,jdbc请求, ...

  6. testng入门教程7 TestNG组测试

    在TestNG中组测试是一个新的创新功能,它不存在于JUnit框架,它允许调度到适当的部分方法和瓶坯复杂的测试方法分组.您不仅可以声明属于群体的那些方法,但你也可以指定一组包含其他组.然后,TestN ...

  7. memcache图形管理工具

    http://www.junopen.com/memadmin/其中有使用截图和介绍 https://github.com/junstor/memadmin 下载地址

  8. Rpgmakermv(5) MiniLabel插件介绍

    ============================================================================ Introduction ========== ...

  9. pandas练习(四)--- 应用Apply函数

    探索学生对酒的消费情况 数据见github 步骤1 - 导入必要的库 import pandas as pd import numpy as np 步骤2 - 数据集 path4 = "./ ...

  10. JsonPath的使用

    语法: JsonPath 描述 $ 根节点 @ 当前节点 .or[] 子节点 .. 选择所有符合条件的节点 * 所有节点 [] 迭代器标示,如数组下标 [,] 支持迭代器中做多选 [start:end ...