C++ find_first_of

之前用C++的字符串查找函数,错用了 find_first_of 等四个函数。实际的 find_first_of函数的作用是:在一个字符串中进行查找,返回值是第一个与指定字符串中任何字符匹配的字符位置;如果没有找到匹配的内容,就返回 string::npos 。
举例如下:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "abcdefg";
    cout << str.find_first_of("edc");
    return 0;
}

输出为2。
在 str 找字符 ‘e’, ‘d’, ‘c’,可以看到最先找到 ‘c’,则返回 ‘c’ 的位置,即 2;

猜你喜欢

转载自blog.csdn.net/gengli2017/article/details/82349366
今日推荐