zoukankan      html  css  js  c++  java
  • c++ string类find总结

    c++ string类的find:
    1.find

    string s = "My cat's breath smells like cat food.";
    int a=s.find("breath");
    cout<<a<<endl;


    因为find是从0数起,输出字符串第一个字符的索引(从0开始),所以a为9

    2.rfind

    string s = "My cat's breath smells like cat food.";
    int a=s.rfind("breath");
    cout<<a<<endl;


    因为rfind是从后部数起,还是输出字符串第一个字符的索引(从0开始)

    其实,find与rfind一样,都可以找完整的字符串和字符,下面来看看另外一些奇葩的函数

    3.find_first_of

    string s = "My cat's breath smells like cat food.";
    int a=s.find_first_of("beah");
    cout<<a<<endl;


    从0数起,但它只需找到一个与它串里一样的字符就输出来了
    答案是:4
    4.find_last_of

    string s = "My cat's breath smells like cat food.";
    int a=s.find_last_of("beah");
    cout<<a<<endl;


    从后部数起,而且和find_first_of一样,输出最后一个有一样的就好了
    答案是: 29

    5.find_first_not_of

    string s = "aaabaaaaaabaa.";
    int a=s.find_first_not_of('a');
    cout<<a<<endl;


    从0数起,但它只需找到一个与它串里不一样的字符就输出来了
    答案是:3

    6.find_last_not_of

    string s = "aaabaaaaaabaa";
    int a=s.find_last_not_of('a');
    cout<<a<<endl;


    从后部数起,而且和find_first_not_of一样,也是输出第一个(似乎是最后一个哈)有一样的就好了
    答案是: 10

    如果没找到的话会输出string::npos

  • 相关阅读:
    F. The Treasure of The Segments
    D. Zigzags
    C. Binary String Reconstruction
    B. RPG Protagonist
    中国计量大学同步赛补题
    Teacher Ma专场补题
    2020ICPC上海站总结&补题
    华东202011月赛补题
    算法学习之旅——树状数组
    迷宫
  • 原文地址:https://www.cnblogs.com/hkaren/p/12356723.html
Copyright © 2011-2022 走看看