Следующая строка кода возвращает true
, только если все элементы из v
отсутствуют в m
:
bool a = v.end() == std::find_if( v.begin(), v.end(), boost::bind( &str_map_t::const_iterator::operator!=, boost::bind<str_map_t::const_iterator>( &str_map_t::find, &m, _1 ), m.end() ) );
Пояснение:
Здесь у нас есть два функтора:
boost::bind<str_map_t::const_iterator>( &str_map_t::find, &m, _1 )
Этот функтор вернет const_iterator
, который указывает на элемент с m
или m.end()
, если не найден. Здесь вы должны явно указать тип возвращаемого значения str_map_t::const_iterator
для boost::bind
, чтобы избавиться от неоднозначности.
boost::bind( &str_map_t::const_iterator::operator!=, _1, _2 )
Этот вернет true
, если _1!=_2
и false
в противном случае.
Объедините 1 и 2, и мы получим полный код:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <map>
#include <boost/bind.hpp>
using namespace std;
int main(int argc, char *argv[])
{
vector<string> v;
v.push_back("x");
v.push_back("a");
v.push_back("6");
typedef map<string, string> str_map_t;
str_map_t m;
m.insert( str_map_t::value_type( "b", "f" ) );
bool a =
v.end() == std::find_if(
v.begin(), v.end(),
boost::bind(
&str_map_t::const_iterator::operator!=,
boost::bind<str_map_t::const_iterator>( &str_map_t::find, &m, _1 ),
m.end()
)
);
std::cout << a << endl;
return 0;
}
Я бы не сказал, что это читаемый код, и я бы порекомендовал написать собственный функтор, чтобы сделать его более читабельным Более читаемая версия может выглядеть следующим образом (без bind
):
struct not_in_map {
not_in_map( const str_map_t& map ) : map_(map) {}
bool operator()( const string& val ) { return map_.end() != map_.find( val ); }
private:
const str_map_t& map_;
};
bool a = v.end() == std::find_if( v.begin(), v.end(), not_in_map(m) );