How to use string::find to find either "+" or "-" in one operation

c++, find, stl, string

Solution

Use `std::string::find_first_of()`:

size_t found = str.find_first_of("+-");

which (from the linked reference page):

Finds the first character equal to one of the characters in the given character sequence. Search begins at pos, i.e. the found character must not be in position preceding pos.

Problem

I want to find the location of `+` or `-` in a complex number , e.g. ``` x + y*i x - y*i ``` Usually , I will do this : ``` int found = str.find("+"); if (found != string::npos) cout << "'+' also found at: " << found << endl; found = str.find("-"); if (found != string::npos) cout << "'-' also found at: " << found << endl; ``` How can I give `find` multiple options to find in a single run ?

Original source