The following line of code returns true
only if all elements from v
are not present in 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() ) );
Explanation:
Here we have two functors:
boost::bind<str_map_t::const_iterator>( &str_map_t::find, &m, _1 )
This functor will return const_iterator
which points to the element from m
or to m.end()
if not found. Here you should explicitly point return type str_map_t::const_iterator
for boost::bind
to get rid of ambiguity.
boost::bind( &str_map_t::const_iterator::operator!=, _1, _2 )
This one will return true
if _1!=_2
and false
otherwise.
Combine 1 and 2 and we'll get the full code:
#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;
}
I wouldn't say it is readable code and I'd recommend to write a custom functor to get it more readable. A more readable version could look like the following (without 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) );