Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

string

find

stl

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 ?

like image 329
JAN Avatar asked Dec 04 '12 10:12

JAN


2 Answers

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.

like image 66
hmjd Avatar answered Sep 28 '22 02:09

hmjd


Use find_first_of().

Here's a reference for the std::string methods. http://www.cplusplus.com/reference/string/string

like image 25
Masked Man Avatar answered Sep 28 '22 02:09

Masked Man