简体   繁体   English

将 arguments 传递到 lambda 比较器时出现转换错误

[英]Conversion Error when passing arguments to lambda comparator

I have a sorted vector of structures and I am trying to find the index of element that has the queried value in its member attribute.我有一个排序的结构向量,我试图找到在其成员属性中具有查询值的元素的索引。 For this I am trying to use lower_bound, but I am having problems with the comparator lambda function.为此,我尝试使用 lower_bound,但比较器 lambda function 出现问题。 Here is a simplified version of my code:这是我的代码的简化版本:

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

struct Student
{
    int id;
};

std::vector<Student> Students;

int main()
{
    Student Ann = {1};
    Student Bob = {3};
    Student Alice = {4};
    Students = {Ann, Bob, Alice};
    int id_query = 3;
    auto index_ptr = std::lower_bound(Students.begin(), Students.end(), id_query, [](const int a, const Student s) -> bool {return a < s.id;});
}

g++ with std=17 gives me the following g++ with std=17 给了我以下

Starting build...
/bin/g++ -g /home/aram/dev/cpp/coverages/stack_overflow.cpp -std=c++17 -o /home/aram/dev/cpp/coverages/stack_overflow
In file included from /usr/include/c++/9/bits/stl_algobase.h:71,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from /home/aram/dev/cpp/coverages/stack_overflow.cpp:1:
/usr/include/c++/9/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >; _Value = const int; _Compare = main()::<lambda(int, Student)>]’:
/usr/include/c++/9/bits/stl_algobase.h:979:14:   required from ‘_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >; _Tp = int; _Compare = __gnu_cxx::__ops::_Iter_comp_val<main()::<lambda(int, Student)> >]’
/usr/include/c++/9/bits/stl_algo.h:2032:32:   required from ‘_FIter std::lower_bound(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >; _Tp = int; _Compare = main()::<lambda(int, Student)>]’
/home/aram/dev/cpp/coverages/stack_overflow.cpp:21:142:   required from here
/usr/include/c++/9/bits/predefined_ops.h:177:11: error: no match for call to ‘(main()::<lambda(int, Student)>) (Student&, const int&)’
  177 |  { return bool(_M_comp(*__it, __val)); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/bits/predefined_ops.h:177:11: note: candidate: ‘bool (*)(int, Student)’ <conversion>
/usr/include/c++/9/bits/predefined_ops.h:177:11: note:   candidate expects 3 arguments, 3 provided
/home/aram/dev/cpp/coverages/stack_overflow.cpp:21:83: note: candidate: ‘main()::<lambda(int, Student)>’
   21 |     auto index_ptr = std::lower_bound(Students.begin(), Students.end(), id_query, [](const int a, const Student s) -> bool {return a < s.id;});
      |                                                                                   ^
/home/aram/dev/cpp/coverages/stack_overflow.cpp:21:83: note:   no known conversion for argument 1 from ‘Student’ to ‘int’

Build finished with error(s).
The terminal process failed to launch (exit code: -1).

The comparator of std::lower_bound is supposed to take the object dereferenced from the iterator, ie the element as the 1st parameter, the value to be compared as the 2nd parameter. std::lower_bound的比较器应该采用从迭代器中解引用的 object,即元素作为第一个参数,要比较的值作为第二个参数。

The type Type1 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type1 .类型Type1必须使得 ForwardIt 类型的ForwardIt可以被取消引用,然后隐式转换为Type1 The type Type2 must be such that an object of type T can be implicitly converted to Type2 . Type2类型必须使得T类型的 object 可以隐式转换为Type2

You need to change the order of the parameters, eg您需要更改参数的顺序,例如

auto index_ptr = std::lower_bound(Students.begin(), Students.end(), id_query, [](const Student s, const int a) -> bool {return s.id < a;});

LIVE居住

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 arguments 传递给线程 function 时,转换何时发生? - When does the conversion happen when passing arguments to thread function? 将函数的引用和lambda表达式作为参数传递时有什么区别? - What is the difference when passing function's reference and lambda expression as arguments? 在lambda内部传递参数时,无法调用成员指针 - Unable to call pointer-to-member when passing arguments inside lambda 传递参数并将其转换为十六进制时出错 - Error when passing arguments and convert it to hexadecimal 将 arguments 传递给 pthread_create() 时出错 - Error when passing arguments to pthread_create() c++:可变参数 function 的两个问题:int 转换错误并通过 arguments - c++: Two issues with variadic function: int conversion error and passing arguments 将字符串传递给函数时出现“转换为const char *”错误 - “conversion to const char*” error when passing string to function C ++将函数参数传递给另一个lambda - C++ passing function arguments to another lambda 使用 std::bsearch 问题没有合适的转换 function 比较器错误 - use of std::bsearch issues no suitable conversion function error for comparator 将lambda传递给STL集时出现错误“错误的函数调用” - Error “bad function call” when passing lambda to STL set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM