2014-04-14 5 views
1

두 개의 컨테이너가 있습니다. 하나는 벡터 유형이고 다른 하나는 unordered_set입니다.하나의 컨테이너에서 요소를 바인딩하여 다른 컨테이너에서 func을 호출하는 방법

벡터의 요소 중 하나가 unordered_set에 존재하는지 여부를 확인하고 싶습니다. find_first_of은 true이고 그에 따라 true/false를 반환합니다. 내가 unordered_set의 발견을 이용하고 싶어하기 때문에

지금, 나는의 생각은 any_of (vector_container.begin(), vector_container.end(), 술어) 대신 find_first_of를 사용를 사용합니다.

내가 사용할 수있는 방법은 boost :: bind 벡터에서 요소를 바인드하여 unordered_set에서 찾을 수 있으므로 술어 클래스를 쓸 필요가 없습니까? find()으로 이렇게

+1

가 왜 Boost.Bind를 사용하려면이 아닌 [람다 식] 않습니다는 (http://en.cppreference.com/w/cpp/language/lambda)? – Mankarse

+0

나는 C++을 사용할 수 없기 때문에 0x – soumeng78

+0

을 사용 했으므로 이제는 ([ any_of는 C++ 98에서 사용할 수 없습니다. 다른 유사한 알고리즘이 있습니까? – soumeng78

답변

0

매우 어색하지만, 대안이있다 : 당신이 unordered_set의 count() 기능을 사용할 수 있습니다

boost::algorithm::any_of(
    vector_container.begin(), vector_container.end(), 
    boost::bind(&boost::unordered_set<int>::count, boost::ref(set_container), _1)); 
0

여기에 "비 존재"로 count(n) == 0를 사용하여, 하나 약간 게으른 술어의을 (그리고 "= = 1 "을"존재 "로 표시) : composability of boost::bind을 사용합니다. 조금 더 장황한 경우 대신 find(n) == end() 또는 find(n) != end()을 사용할 수 있습니다.

여기 벡터에서 세트에있는 모든 요소를 ​​제거하는 약간의 데모입니다 :

#include <boost/bind.hpp> 
#include <unordered_set> 
#include <algorithm> 
#include <functional> 
#include <iostream> 
#include <vector> 

int main() 
{ 
    std::unordered_set<int> s { 1, 2, 3, 4 }; 
    std::vector<int> v { 2, 5, 9, 1 }; 

    v.erase(
     std::remove_if(
      v.begin(), v.end(), 
      boost::bind(
       std::equal_to<std::size_t>(), 
       boost::bind(std::mem_fun(&std::unordered_set<int>::count), &s, _1), 
       1)), 
     v.end()); 

    for (int n : v) { std::cout << n << "\n"; } 
} 
0

std::any_of(v.begin(), v.end(), boost::bind(&std::set<int>::find, &s, boost::lambda::_1) != s.end());