使用 std::placeholders 需要哪些库?
目前我正在尝试生成组合,并且正在使用以下代码:
#include <vector>
#include <algorithm>
#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>());
}
使用 g++ 无法编译说:
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
我认为 std::placeholders 是在函数中声明的,但现在我很困惑。 我应该只使用boost吗?
另外,该项目的其余部分正在使用 c++0x,那么有没有更好的方法使用 c++0x 功能来编写此代码?
非常感谢任何帮助:)
Currently I am trying to generate combinations and I am using the following code:
#include <vector>
#include <algorithm>
#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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然还有更好的方法。使用 std::greater_equal 而不是执行上述操作。检查此处泛函中还有哪些内容。
忘了说 - 要使用占位符,您需要启用 c++0x 功能。
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.
为什么不使用 lambda 来代替麻烦的绑定呢?如果您使用 C++0x,则编写
std::sort(v.begin(), v.end(), [](int l, int r)->bool { return l > 是有意义的; r; });
代替。请注意,您不需要在那里显式指定返回类型。我这样写只是为了更清楚。
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.