std::bind 不适用于 std::sort
std::bind do not work with std::sort
为什么只有当第二个参数大于 3 时它才有效。我该如何解决?
如果我对 copy_if 做同样的事情,它就会起作用!
任务:检查仿函数 std::bind 的效果。尝试用它来构成标准仿函数 std::greater(module) 的条件。
#include <set>
#include <algorithm>
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <iterator>
#include <string>
#include <functional>
using namespace std;
template<typename T>
static void PrintVector(const std::vector<T> &v)
{
for (auto iterator = v.begin(); iterator != v.end(); ++iterator)
{
std::cout << *iterator << " ";
}
std::cout << std::endl;
}
int main()
{
std::cout << "Task_3: greater with std::bind\n";
ostream_iterator<int> out_it(cout, " ");
vector<int> v = { 1, 8, 7, 4, 3, 6, 2, 5 };
PrintVector(v);
auto greater_binded = bind(greater<int>(), placeholders::_1, 3);
sort(v.begin(), v.end(), greater_binded);
PrintVector(v);
return 0;
}
如documentation for std::copy_if
it expects unary predicate ie function with one argument, on another side std::sort
needs compare function, that must meet requirement of Compare 概念所述。所以完全不清楚为什么你期望用于 std::copy_if
的相同函数与 std::sort
.
一起使用
And how can I fix it?
只需传递 std::greater<int>
而无需将第二个参数绑定到常量。如果你确实需要使用 std::bind
你可以只传递两个参数:
auto greater_binded = bind(greater<int>(), placeholders::_1, placeholders::_2);
但这与直接传递 greater<int>()
具有相同的效果。
sort
的比较器要求是:
Comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true
if the first argument is less than (i.e. is ordered before) the second
greater
functor takes 2 arguments, but you are using bind
使其成为一个带有 1 个参数并将其与 3
进行比较的仿函数。这不再满足 sort
的要求。
如果您尝试将所有大于 3
的元素移动到 v
的前面,您可以使用 partition
with greater_binded
. Calling partition(begin(v), end(v), greater_binded)
results in:
5 8 7 4 6 3 2 1
您还可以进一步使用 partition
的 return,它是:
Iterator to the first element of the second group
使用它来排序或 reverse-sort 第 1st 或 2nd 组 sort
.
为什么只有当第二个参数大于 3 时它才有效。我该如何解决? 如果我对 copy_if 做同样的事情,它就会起作用! 任务:检查仿函数 std::bind 的效果。尝试用它来构成标准仿函数 std::greater(module) 的条件。
#include <set>
#include <algorithm>
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <iterator>
#include <string>
#include <functional>
using namespace std;
template<typename T>
static void PrintVector(const std::vector<T> &v)
{
for (auto iterator = v.begin(); iterator != v.end(); ++iterator)
{
std::cout << *iterator << " ";
}
std::cout << std::endl;
}
int main()
{
std::cout << "Task_3: greater with std::bind\n";
ostream_iterator<int> out_it(cout, " ");
vector<int> v = { 1, 8, 7, 4, 3, 6, 2, 5 };
PrintVector(v);
auto greater_binded = bind(greater<int>(), placeholders::_1, 3);
sort(v.begin(), v.end(), greater_binded);
PrintVector(v);
return 0;
}
如documentation for std::copy_if
it expects unary predicate ie function with one argument, on another side std::sort
needs compare function, that must meet requirement of Compare 概念所述。所以完全不清楚为什么你期望用于 std::copy_if
的相同函数与 std::sort
.
And how can I fix it?
只需传递 std::greater<int>
而无需将第二个参数绑定到常量。如果你确实需要使用 std::bind
你可以只传递两个参数:
auto greater_binded = bind(greater<int>(), placeholders::_1, placeholders::_2);
但这与直接传递 greater<int>()
具有相同的效果。
sort
的比较器要求是:
Comparison function object (i.e. an object that satisfies the requirements of Compare) which returns
true
if the first argument is less than (i.e. is ordered before) the second
greater
functor takes 2 arguments, but you are using bind
使其成为一个带有 1 个参数并将其与 3
进行比较的仿函数。这不再满足 sort
的要求。
如果您尝试将所有大于 3
的元素移动到 v
的前面,您可以使用 partition
with greater_binded
. Calling partition(begin(v), end(v), greater_binded)
results in:
5 8 7 4 6 3 2 1
您还可以进一步使用 partition
的 return,它是:
Iterator to the first element of the second group
使用它来排序或 reverse-sort 第 1st 或 2nd 组 sort
.