std :: bind不适用于std :: sort

阿西亚·菲拉托娃(Asya Filatova)

为什么只有在第二个参数大于3时它才起作用?我该如何解决?如果我对copy_if做同样的工作!任务:检查函子std :: bind的效果。尝试使用它来形成标准函子std :: Greater(模块)的条件。

#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;
}
斯拉瓦

文档中所述std::copy_if它期望一元谓词,即具有一个参数的std::sort函数,另一方面需要比较函数,必须满足比较概念的要求为什么你希望使用相同的功能,因此是完全不清楚std::copy_if同工作std::sort

我该如何解决?

只需传递std::greater<int>而不将第二个参数绑定到常量即可。如果确实需要使用std::bind,则可以只传递两个参数:

auto greater_binded = bind(greater<int>(), placeholders::_1, placeholders::_2);

但这与greater<int>()直接传递具有相同的效果

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

std::bind 不适用于参考?

std :: merge不适用于std :: async

重载的operator <<模板不适用于std :: list,尽管适用于std :: vector

从 BST 中删除节点不适用于 std::pair

std :: ofstrean不适用于-O3

std :: map查找不适用于C ++

lang不适用于std :: experimental :: optional

比率数学不适用于std :: chrono :: duration

用户定义的容器不适用于std :: ranges

javaout typemap不适用于指针的std :: vector

std :: cin适用于少量的行,但不适用于较大的行

指向成员的指针的语法糖适用于数组,但不适用于std :: vector

索引 std::vector<bool> 适用于真值,但不适用于假值

M_PI不适用于gcc --std = c11但适用于--std = gnu11?

std :: for_each和std :: execution :: par_unseq不适用于GCC,但适用于MSVC

std :: stable_sort与std :: sort

std :: erase和std :: remove组合以删除特定元素不适用于特定示例

.sort不适用于Firefox

std::chrono 纳秒计时器适用于 MSVC 但不适用于 GCC

bug in std :: sort?

QuickSort比std :: sort慢

为什么std :: begin()和std :: end()可用于固定数组,但不适用于动态数组?

使用名称空间和using指令不适用于std :: enable_if_t

为什么std :: is_assignable不适用于原始类型?(确认)

尽管实现了专用的非模板功能,std :: endl不适用于重载的运算符<<

为什么`return {};`不适用于`std :: forward_list`?

为什么 std::function 不适用于函数模板?

为什么 std::shared_ptr 不适用于基本模板类?

std :: equal_range不适用于具有operator <定义的strucutre