views:

76

answers:

4
std::vector<std::wstring> lines;
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.

+3  A: 

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""));
KennyTM
I still like the mem_fun_ref solution better, but this works. +1.
Billy ONeal
+3  A: 

Use boost::lambda and boost::bind and define it as bind(&std::wstring::size, _1))

UncleZeiv
That will tell you strings which are NOT empty, not strings which ARE empty.
Billy ONeal
Isn't he asking for string NOT empty? His code suggests so.
UncleZeiv
@UncleZeiv: Good point. Doh!
Billy ONeal
+3  A: 

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.

dark_charlie
+5  A: 

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)));
Billy ONeal