如何排序STL向量 [英] how to sort STL vector
本文介绍了如何排序STL向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要对向量排序
vector<myclass> object;
其中,由于myclass包含许多int数据变量,如何对任何特定数据变量
where as myclass contains many int data variables, how can i sort my vector on any specific data variable of myclass.
推荐答案
重载小于运算符,然后排序。这是我在网络上找到的示例...
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());
资料来源:这里
这篇关于如何排序STL向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文