我有一个功能对象test_id
和一个结构Node
:
#include <functional>
#include <iterator>
#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>
#include <cstdlib>
struct test_id : std::unary_function< int, bool>
{
typedef std::vector<int>::const_iterator iterator;
iterator first, last;
explicit test_id(std::vector<int> const& v_id) : first(v_id.begin()), last(v_id.end()){}
bool operator()(int id)const{ return std::binary_search(first, last, id); }
};
struct Node
{
int id;
std::string name;
//...and a lot of members.
//-------------------
std::vector< std::unique_ptr<Node> > children;
};
int main()
{
Node node;
// initialize it with some values
node.id = 0;
node.name = "root";
srand(time(NULL));
for(int i= 0; i < 100; ++i)
{
std::unique_ptr<Node> ch (new Node );
ch->id = static_cast<int>( rand() / (double)RAND_MAX * 100 );
ch->name = "children";
node.children.push_back( std::move(ch) );
}
//............
std::vector<int> existing_ids;
// initialize it with some values.
for(int i = 0; i < 100; ++i)
if ( i % 3 == 1) existing_ids.push_back(i);
//sort it
std::sort(existing_ids.begin(), existing_ids.end() );
// Now, I want remove elements from the children, where id is in existing_ids vector.
node.children.erase(
std::remove_if(
node.children.begin(),
node.children.end(),
std::bind(test_id(existing_ids), std::bind(&Node::id, std::placeholders::_1) )//--> compile error
),
node.children.end() );
}
我使用Visual Studio 2010 sp1。 我知道我可以使用lambda,但我的问题是:在这种情况下如何使用std :: bind?
编辑:这个编译很好,所以我可以从Node中提取id:
//EDIT:
std::vector<int> all_ids;
std::transform(
node.children.begin(),
node.children.end(),
std::back_inserter(all_ids),
std::bind(&Node::id, std::placeholders::_1)
);