How to sort an STL vector?

前端 未结 5 2288
借酒劲吻你
借酒劲吻你 2020-12-04 10:44

I would like to sort a vector

vector object;

Where myclass contains many int variabl

5条回答
  •  猫巷女王i
    2020-12-04 11:19

    Overload less than operator, then sort. This is an example I found off the web...

    class MyData
    {
    public:
      int m_iData;
      string m_strSomeOtherData;
      bool operator<(const MyData &rhs) const { return m_iData < rhs.m_iData; }
    };
    
    std::sort(myvector.begin(), myvector.end());
    

    Source: here

提交回复
热议问题