1. 程式人生 > >QVector 排序結構體資料 QString、int

QVector 排序結構體資料 QString、int

#include <QCoreApplication>
#include <vector>
#include <string>
#include <iostream>

#include <QVector>
using namespace std;
typedef struct devInfo_
{
    int num;
    QString name;
}devInfo;

bool comQt(devInfo&a, devInfo&b)
{
    if (a.num > b.num)
    {
        return
true; } else { return false; } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); devInfo dev0; devInfo dev1; dev0.num = 51; dev0.name = "DEV01100025"; dev1.num = 4; dev1.name = "DEV01100025"; /****************qt qsort vector 排序 Start ***********************/
QVector<devInfo> mQVec; mQVec.push_back((dev0)); mQVec.push_back((dev1)); qSort(mQVec.begin(), mQVec.end(), comQt); QVector<devInfo>::iterator it1; for(it1 = mQVec.begin(); it1 != mQVec.end(); it1 ++) { devInfo dev = *it1; cout << dev.name << endl; } /****************qt qsort vector 排序 End ***********************/
return a.exec(); }