C++ 如何显示向量的最大元素<;myStruct>;?

C++ 如何显示向量的最大元素<;myStruct>;?,c++,algorithm,vector,operator-overloading,max,C++,Algorithm,Vector,Operator Overloading,Max,我有一个源代码,包含如下内容: #include <iostream> #include <algorithm> #include <vector> using namespace std; struct st { int id; double d; }; bool comp(int a, int b) { return (a < b); } bool comp2(st &a, st& b)

我有一个源代码,包含如下内容:

#include <iostream> 
#include <algorithm> 
#include <vector>
using namespace std; 

struct st
{
    int id;
    double d;
}; 

bool comp(int a, int b) 
{ 
    return (a < b); 
} 

bool comp2(st &a, st& b) { return a.d < b.d; }

int main() 
{
    vector<int> v = { 9, 4, 7, 2, 5, 15, 11, 12, 1, 3, 6 };
    vector<st> vec = {{1, 5.6},{2, 5.7},{3, 4.3}};

    auto max = std::max_element(v.begin(), v.end(), comp);
    cout << "Max element of v: " << *max; // It's working

    auto max2 = std::max_element(vec.begin(), vec.end(), comp2);
    cout << "Max element of vec: " << *max2; // It's not working

    return 0; 
}

问题的可能解决方案是什么?

您没有定义运算符您没有定义运算符1)这不是一条完整的错误消息。2) 你的
st
没有
操作符提示:试着一次只关注一件事。你当前的问题不是向量,也不是最大值。对于
stx,您会得到类似的错误;这不是一条完整的错误消息。2) 你的
st
没有
操作符提示:试着一次只关注一件事。你当前的问题不是向量,也不是最大值。对于
stx,您会得到类似的错误;标准::cout
[Error] cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
cout << "Max element of vec: " << *max2;
cout << "Max element of vec: " << max2->id << ", " << max2->d << '\n';
std::ostream & operator <<( std::ostream &os, const st &obj )
{
    return os << "( " << obj.id << ", " << obj.d << " )";
}