C ++ vectors - using find (start, end, term)

Ok I do this and it works great.

end = std::find(arToken.begin() + nStart, arToken.end(), ".");

      

I want to expand. include! and? so it finds periods (.), exclamation mark (!) and question mark (?).

Should I use regex in terms?

TIA

+2


source to share


3 answers


you should use std::find_first_of

:



std::string m(".!?");
end = std::find_first_of(arToken.begin() + nStart, arToken.end(), m.begin(),m.end());

      

+14


source


You can use this .std::find_first_of



end=arToken.find_first_of(".!?",nStart);

      

+3


source


use the predicate and std::find_if

like this:

struct has_char {
    has_char(const char *s) : str(s) {}
    bool operator() (const char ch) const {
        return str.find(ch) != std::string::npos;
    }
private:
    std::string str;
};

end = std::find_if(arToken.begin() + nStart, arToken.end(), has_char(".!?"));

      

+2


source







All Articles