使用std :: placeholder需要哪些库?

shu*_*e87 8 c++ bind

目前我正在尝试生成组合,我使用以下代码:

#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>());
}
Run Code Online (Sandbox Code Playgroud)

使用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
Run Code Online (Sandbox Code Playgroud)

我认为std :: placeholders是在功能上声明的,但现在我很困惑.我应该只使用提升吗?

该项目的其余部分也使用c ++ 0x,那么有更好的方法使用c ++ 0x功能编写它吗?

任何帮助是极大的赞赏 :)

BЈо*_*вић 7

当然还有更好的方法.使用std :: greater_equal而不是执行上述操作.检查这里还有什么功能.

忘了说 - 要使用占位符,你需要启用c ++ 0x功能.


Kir*_*kov 5

为什么不使用lambdas而不是繁琐的绑定?如果你使用C++ 0x,它有写意义

std::sort(v.begin(), v.end(), [](int l, int r)->bool { return l > r; });

代替.请注意,您不需要在那里明确指定返回类型.我这样写是为了让它更清晰.