zoukankan      html  css  js  c++  java
  • string类型的查找

    sting s;

    记住典型的查找操作:

    s.find(args)                   

    s.find_fist_of(args)

    s.find_fist_not_of(args)

    还有3个逆序的查找操作,

    s.rfind(args)

    s.find_last_of(args)

    s.find_last_not_of(args)

    其中args可以为下面4个版本之一:

    c,pos

    s2,pos

    cp,pos

    cp,pos,n

    例子:

    /******************************************************************************
     * str.find(args)是完全匹配args中的字符串;
     * 注意:args是参数表(含要查找的字符或字符串参数),不是字符串!
     ********************************************************************************/
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    int main()
    {
    	string str = "helloWorLD";
    	string::size_type pos1 = str.find("Wor");
    	cout << pos1 << endl;
    
    	string::size_type pos2 = str.find("woa");
    	if (pos2 == string::npos) {
    		cout << "Do not find" << endl;
    	} else {
    		cout << pos2 << endl;;
    	}
    	return 0;
    }
    

    输出:

    5

    Do not find

    /******************************************************************************
     * str.find_first_of(args)只要在str中找到args中的字符串参数中的任意字符就返回;
     * 注意:args是参数表(含要查找的字符或字符串参数),不是字符串!
     ********************************************************************************/
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string str = "he2l3l5oWo6rLD";
    	string::size_type pos = str.find_first_of("0123456789");
    	cout << pos << endl;
    	return 0;
    }
    

    输出:

    2

  • 相关阅读:
    P6406 [COCI2014-2015] Norma 分治+数学
    CF547D Mike and Fish 欧拉回路
    P6628 [省选联考 2020 B 卷] 丁香之路 欧拉路+最小生成树
    2020 CSP-S2 游记
    CF594D REQ 树状数组+质因数分解
    CF416E President's Path floyd
    CF1385F Removing Leaves 拓扑排序
    CF449C Jzzhu and Apples 思维题
    回溯法与八皇后问题
    codewars-7kyu:Sum of the first nth term of Series
  • 原文地址:https://www.cnblogs.com/helloweworld/p/2824215.html
Copyright © 2011-2022 走看看