tags:

views:

221

answers:

3

Ok, i'm doing this and it works fine.

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

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

Should i use regex in the term?

TIA

+2  A: 

use a 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(".!?"));
Evan Teran
Can you write that as if it was free function, so I could understand little easier. Thanks
Jeremiah
since you're using std::string, you may want to stick with the methods that class provides. I.e. in your operator() method, you can "return s.find(ch) != npos;"
Tim
also, to be strict about it, has_char should inherit from public std::unary_function<const char, bool> -- which will require #include<functional>
Tim
@jerimiah: it can't be a free function cause it has state. Using a class with an overloaded `operator()` is the proper way to go if you use `find_if`
Evan Teran
@Tim: it is usually a good idea to inherit from `std::unary_function<>`, but is only really required if you want to use binders and such. For the simple case of a predicate, you can usually get away with not doing that.
Evan Teran
+13  A: 

you should use find_first_of:

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

You can use this. std::find_first_of

end=arToken.find_first_of(".!?",nStart);
Aviator
Shouldn't that be std::find_first_of?
Steve314
Yes it is. :). Thanks! Edited my answer.
Aviator