I've written a small snippet like this:
#include <string>
#include <iostream>
int main() {
std::string s1{"abcdefghijklmnop"};
std::cout << "s1(fg) = " << s1.find_first_of("fg") << '\n';
std::cout << "s1(fo) = " << s1.find_first_of("fo") << '\n';
}
I expect that it returns 5 and string::npos, but it actually returns
s1(fg) = 5
s1(fo) = 5
Why does it work like this?
My compile flags and g++ version:
$ g++ -std=c++11 main.cpp
$ g++ --version
g++.exe (Rev1, Built by MSYS2 project) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
std::string::find_first_of
returns the position of any of the characters you supply to it. In your case, it finds f
both times.
You should be using std::string::find
.