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)
이 functor는 m
또는 찾을 수없는 경우 m.end()
의 요소를 가리키는 const_iterator
을 반환합니다. 여기서 명확하게 반환 유형 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));
예, 그런 코드는 일광을 보게하지만 여전히이 멋진 기능을 배우고 싶습니다. 좋은 답변 주셔서 감사합니다. – SWKK