Currently I am trying to generate combinations and I am using the following code:
#include <vector>
#include <algorithm&g开发者_如何学运维t;
#include <iostream>
#include <functional>
template<class RandIt, class Compare>
bool next_combination(RandIt first, RandIt mid, RandIt last)
{
std::sort(mid, last, std::bind(std::less<int>(), std::placeholders::_2
, std::placeholders::_1));
return std::next_permutation(first, last, std::less<int>());
}
Using g++ it fails to compile saying:
next_combo.cpp: In function ‘bool next_combination(RandIt, RandIt, RandIt)’:
next_combo.cpp:10: error: ‘bind’ is not a member of ‘std’
next_combo.cpp:10: error: ‘std::placeholders’ has not been declared
next_combo.cpp:11: error: ‘std::placeholders’ has not been declared
I thought that std::placeholders were declared in functional, but now I'm confused. Should I just use boost?
Also the rest of the project is using c++0x, so is there a better way to write this using c++0x features?
Any help is greatly appreciated :)
Off course there is a better way. Use std::greater_equal instead of doing the above. Check here what else there is in the functional.
Forgot to say - to use placeholder, you need to enable c++0x features.
why don't you use lambdas instead of cumbersome bind? if you use C++0x, it has sense to write
std::sort(v.begin(), v.end(), [](int l, int r)->bool { return l > r; });
instead. Note, that you don't need to specify the return type explicitly there. I wrote this way just to make it clearer.
精彩评论