C++ 如何在C+中使用std::max和自定义比较器+;11?

C++ 如何在C+中使用std::max和自定义比较器+;11?,c++,c++11,C++,C++11,我有一个希尔结构的向量,我想找到一个高度最高的。这是我的密码: #include <vector> #include <algorithm> #include <assert.h> struct Hill { int height; int changed; }; int main() { std::vector<Hill> hills(100); hills[0].height = 100; hills[1

我有一个希尔结构的向量,我想找到一个高度最高的。这是我的密码:

#include <vector>
#include <algorithm>
#include <assert.h>
struct Hill {
    int height;
    int changed;
};
int main() {
    std::vector<Hill> hills(100);
    hills[0].height = 100;
    hills[1].height = 150;
    auto byHeight = [&](const Hill& a, const Hill& b) {
        return a.height < b.height;
    };
    Hill hill = std::max(hills.begin(), hills.end(), byHeight);
    assert(hill.height == 150);
}
#包括
#包括
#包括
结构山{
内部高度;
int改变;
};
int main(){
标准:病媒丘(100);
山丘[0],高度=100;
山丘[1],高度=150;
自动按高度=[&](恒坡和a、恒坡和b){
返回a.高度
但它未能编译:

mcve.cpp:15:10: error: no viable conversion from 'const
      std::__1::__wrap_iter<Hill *>' to 'Hill'
    Hill hill = std::max(hills.begin(), hills.end(), byHeight);
         ^      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mcve.cpp:4:8: note: candidate constructor (the implicit copy constructor) not
      viable: no known conversion from 'const std::__1::__wrap_iter<Hill *>' to
      'const Hill &' for 1st argument
struct Hill {
       ^
mcve.cpp:4:8: note: candidate constructor (the implicit move constructor) not
      viable: no known conversion from 'const std::__1::__wrap_iter<Hill *>' to
      'Hill &&' for 1st argument
struct Hill {
       ^
In file included from mcve.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/vector:270:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/__bit_reference:15:
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:2627:12: error: 
      no matching function for call to object of type '(lambda at
      mcve.cpp:12:21)'
    return __comp(__a, __b) ? __b : __a;
           ^~~~~~
mcve.cpp:15:22: note: in instantiation of function template specialization
      'std::__1::max<std::__1::__wrap_iter<Hill *>, (lambda at mcve.cpp:12:21)>'
      requested here
    Hill hill = std::max(hills.begin(), hills.end(), byHeight);
                     ^
mcve.cpp:12:21: note: candidate function not viable: no known conversion from
      'const std::__1::__wrap_iter<Hill *>' to 'const Hill' for 1st argument
    auto byHeight = [&](const Hill& a, const Hill& b) {
                    ^
2 errors generated.
mcve.cpp:15:10:错误:从“const”没有可行的转换
std::uuu 1::uu包裹到“希尔”
Hill-Hill=std::max(hills.begin()、hills.end()、byHeight);
^      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mcve.cpp:4:8:注意:候选构造函数(隐式副本构造函数)不是
可行:未知从“const std::\u 1::\u wrap\u iter”到
第一个参数为“const-Hill&”
结构山{
^
mcve.cpp:4:8:注意:候选构造函数(隐式移动构造函数)不是
可行:未知从“const std::\u 1::\u wrap\u iter”到
第一个参数为“Hill&”
结构山{
^
在mcve.cpp中包含的文件中:1:
在/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:270中包含的文件中:
在/Library/Developer/CommandLineTools/usr/include/c++/v1/_ubit_参考:15中包含的文件中:
/Library/Developer/CommandLineTools/usr/include/c++/v1/算法:2627:12:错误:
对类型为“”的对象的调用没有匹配函数
mcve.cpp:12:21)'
返回公司(\uuuuu a,\uuuuu b)?\uuuuuuu b:\uuuuu a;
^~~~~~
mcve.cpp:15:22:注意:在函数模板专门化的实例化中
'std::_1::max'
在此请求
Hill-Hill=std::max(hills.begin()、hills.end()、byHeight);
^
mcve.cpp:12:21:注意:候选函数不可行:没有已知的从
第一个参数的“const std::u 1::u wrap_iter”到“const Hill”
自动按高度=[&](恒坡和a、恒坡和b){
^
产生2个错误。

如何修复它?

更改这两行代码修复了问题(感谢@milleniumbug):


*std::max_元素
获取元素本身。
std::max_元素
返回一个迭代器,给您修改元素的机会。

std::max
更改为
*std::max_元素
,它会工作。使用
*


max_元素()
返回一个迭代器,
*
取消引用以获取实际元素。

投票以键入方式结束。您需要
std::max_元素
,而不是
std::max
@NathanOliver打开,我看到一个带有自定义比较器和std::max.@NathanOliver的示例,顺便说一句,
std::max_元素
也无法编译。
 std::max_元素
返回一个迭代器,因此如果您只想更改它,它当然不会编译。顺便说一句,std::max不会工作,因为它是用来比较两个元素,而不是向量的迭代器。我不知道std::max_元素会出现什么错误,但这可能是另一个问题。
    auto hill = std::max_element(hills.begin(), hills.end(), byHeight);
    assert(hill->height == 150);