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 ?
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.
Use find_first_of()
.
Here's a reference for the std::string
methods.
http://www.cplusplus.com/reference/string/string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With