2010-08-20 12 views
2
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()); 

Comment définir is_str_empty? Je ne crois pas que boost l'approvisionne.C++ is_str_empty prédicat

Répondre

6

Utilisez mem_fun/mem_fun_ref:

iterator_t eventLine = std::find_if(lines.begin(), lines.end(), 
    std::mem_fun_ref(&std::wstring::empty)); 

Si vous voulez, quand la chaîne est pas vide, alors:

iterator_t eventLine = std::find_if(lines.begin(), lines.end(), 
    std::not1(std::mem_fun_ref(&std::wstring::empty))); 
3

Pure STL est suffisant.

#include <algorithm> 
#include <functional> 

... 

iterator_t eventLine = std::find_if(lines.begin(), lines.end(), 
           std::bind2nd(std::not_equal_to<std::wstring>(), L"")); 
+0

Je aime toujours la solution mem_fun_ref mieux, mais cela fonctionne. +1 –

3

Utiliser boost :: lambda et boost :: bind et définir comme bind(&std::wstring::size, _1))

+0

Cela vous dira des chaînes qui ne sont pas vides, pas des chaînes qui sont vides. –

+0

Ne demande-t-il pas une chaîne NON vide? Son code le suggère. – UncleZeiv

+0

Bon point. Doh! –

3

Vous pouvez utiliser un foncteur:

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 

Notez que si vous voulez une négation, vous devez changez le foncteur:

struct is_str_not_empty { 
    bool operator() (const std::wstring& s) const { return !s.empty(); } 
}; 

Ou utilisez simplement find comme suggéré par KennyTM.