C++ 如何将一个容器中的元素绑定到另一个容器中的成员func
我有两个容器-一个是向量类型,另一个是无序集 现在,我想检查向量中的任何元素是否存在于无序的集合中,比如find\u first\u ofdoes,并相应地返回true/false 现在,由于我想利用无序集的find,我想使用(vector\u container.begin(),vector\u container.end(),predicate)的任意而不是使用find\u first\u 有没有一种方法可以让我使用boost::bind来绑定向量中的元素,以便从无序集进行查找,这样我就不必编写谓词类了?使用C++ 如何将一个容器中的元素绑定到另一个容器中的成员func,c++,boost-bind,stl-algorithm,C++,Boost Bind,Stl Algorithm,我有两个容器-一个是向量类型,另一个是无序集 现在,我想检查向量中的任何元素是否存在于无序的集合中,比如find\u first\u ofdoes,并相应地返回true/false 现在,由于我想利用无序集的find,我想使用(vector\u container.begin(),vector\u container.end(),predicate)的任意而不是使用find\u first\u 有没有一种方法可以让我使用boost::bind来绑定向量中的元素,以便从无序集进行查找,这样我就不必
find()
进行此操作非常尴尬,但还有一种选择:可以使用无序集的count()
函数:
boost::algorithm::any_of(
vector_container.begin(), vector_container.end(),
boost::bind(&boost::unordered_set<int>::count, boost::ref(set_container), _1));
boost::algorithm::任意(
vector_container.begin(),vector_container.end(),
boost::bind(&boost::无序的集合::计数,boost::ref(集合容器),_1));
这里有一个稍微懒惰的谓词,使用count(n)==0
作为“不存在”(而“==1”作为“存在”):
std::任意(v.begin()、v.end()、boost::bind(&std::set::find、&s、boost::lambda::1)!=s.end())代码>为什么要使用Boost.Bind而不是a?(也就是说,[&](vector_container::value_type const&v){return set_container.find(v)!=set_container.end();}
),因为我无法使用C++0xI现在意识到,即使是任何_of,在C++98中也不可用。还有其他类似的算法吗?
boost::bind(
std::equal_to<std::size_t>(),
boost::bind(std::mem_fun(&std::unordered_set<int>::count), &s, _1),
0)
#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"; }
}