std::vector<std::wstring> li开发者_JAVA技巧nes;
typedef std::vector<std::wstring>::iterator iterator_t;
iterator_t eventLine = std::find_if(lines.begin(), lines.end(), !is_str_empty());
how do I define is_str_empty? i don't believe boost supplies it.
Use mem_fun / mem_fun_ref:
iterator_t eventLine = std::find_if(lines.begin(), lines.end(),
std::mem_fun_ref(&std::wstring::empty));
If you want when the string is NOT empty, then:
iterator_t eventLine = std::find_if(lines.begin(), lines.end(),
std::not1(std::mem_fun_ref(&std::wstring::empty)));
Pure STL is enough.
#include <algorithm>
#include <functional>
...
iterator_t eventLine = std::find_if(lines.begin(), lines.end(),
std::bind2nd(std::not_equal_to<std::wstring>(), L""));
Use boost::lambda and boost::bind and define it as bind(&std::wstring::size, _1))
You can use a functor:
struct is_str_empty {
bool operator() (const std::wstring& s) const { return s.empty(); }
};
std::find_if(lines.begin(), lines.end(), is_str_empty()); // NOTE: is_str_empty() instantiates the object using default constructor
Note that if you want a negation, you have to change the functor:
struct is_str_not_empty {
bool operator() (const std::wstring& s) const { return !s.empty(); }
};
Or just use find as suggested by KennyTM.
In c++17 std::unary_function
is removed and thus also std::not1
and std::mem_fun_ref
(not that modern compilers care much about that...).
Using a lambda would be probably the most natural way now, i.e.
auto eventLine = std::find_if(lines.begin(), lines.end(),
[](const std::wstring &s){return !s.empty();});
In order to keep the structure of @BillyONeal's answer one would have to use std::not_fn
and std::mem_fn
, i.e.:
auto eventLine = std::find_if(lines.begin(), lines.end(),
std::not_fn(std::mem_fn(&std::wstring::empty)));
精彩评论