如何修复“错误:‘_1’未在此范围内声明"? [英] How to fix "error: ‘_1’ was not declared in this scope"?

查看:106
本文介绍了如何修复“错误:‘_1’未在此范围内声明"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将函数绑定到我打算使用的算法.

I'm currently trying to bind a function to an algorithm I'm intending to use.

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
    vector<int> coll{1, 2, 3, 4, 5, 6};

    vector<int>::iterator pos;
    pos = find_if (coll.begin(), coll.end(), 
                    bind(greater<int>(),_1,3)); 

    return 0;
}

并获得此错误反馈:

AlgoTesting.cpp: In function ‘int main()’:
AlgoTesting.cpp:184:41: error: ‘_1’ was not declared in this scope
                     bind(greater<int>(),_1,3)); // criterion
                                         ^~
AlgoTesting.cpp:184:41: note: suggested alternative:
In file included from algostuff.hpp:15:0,
                 from AlgoTesting.cpp:5:
/usr/include/c++/7/functional:275:34: note:   ‘std::placeholders::_1’
     extern const _Placeholder<1> _1;
                                  ^~

我正在努力理解错误日志的含义.有谁知道我在这里错过了什么?

I'm struggling to understand what the error log means. Does anyone know what I'm missing here?

推荐答案

您需要包含 并使用 std::placeholders::_1

pos = find_if (coll.begin(), coll.end(), bind(greater<int>(),placeholders::_1,3));

更简单的选择是使用 lambda:

An easier option is to use a lambda:

pos = find_if(coll.begin(), coll.end(), [](int v) { return std::greater<int>{}(v, 3); });

pos = find_if(coll.begin(), coll.end(), [](int v) { return 3 < v; });

这篇关于如何修复“错误:‘_1’未在此范围内声明"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆