#include <iostream> #include <vector> #include <algorithm> class MyData { public: int m_iData; bool operator<(MyData rhs) { return m_iData < rhs.m_iData; } }; int main () { std:: vector <MyData> myvector(2,MyData() ); myvector[0].m_iData=2; myvector[1].m_iData=4; std::sort(myvector.begin(),myvector.end()); }
试图编译这个给出:
error: passing 'const MyData' as 'this' argument of 'bool MyData::operator<(MyData)' discards qualifiers