c++ 排序一个vector 和 QVector 排序操作

#include 
#include 
#include 
#include 

#include 
using namespace std;
typedef struct stuInfo_
{
    int num;
    string name;
}stuInfo;


int com(const void *a, const void *b)
{

    stuInfo * aa;
    stuInfo*       bb;
    aa = (stuInfo *)a;
    bb = (stuInfo *)b;
    return aa->num > bb->num?1:-1;
}

bool comQt(stuInfo &a, stuInfo &b)
{
    if (a.num > b.num)
    {
        return true;
    }
    else
    {
        return false;
    }
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

/****************std c++ qsort vector 排序 Start ***********************/
    stuInfo stu0;
    stuInfo stu1;
    stuInfo stu2;
    stuInfo stu3;

    stu0.num = 3;
    stu0.name = "wang";

    stu1.num = 4;
    stu1.name = "li";

    stu2.num = 2;
    stu2.name = "zhang";

    stu3.num = 7;
    stu3.name = "zhao";


    std::vector mVec;

    mVec.push_back((stu0));

    mVec.push_back((stu1));

    mVec.push_back((stu2));

    mVec.push_back((stu3));

    vector::iterator it;
    for(it = mVec.begin(); it != mVec.end(); it ++)
    {
        stuInfo stu = *it;
        cout << stu.num << endl;
    }

    qsort(&(mVec.data()[0]), mVec.size(), sizeof(stuInfo), com);

    for(it = mVec.begin(); it != mVec.end(); it ++)
    {
        stuInfo stu = *it;
        cout << stu.name << endl;
    }
/****************std c++ qsort vector 排序 End***********************/

 /****************qt qsort vector 排序 Start ***********************/
    QVector mQVec;

    mQVec.push_back((stu0));

    mQVec.push_back((stu1));

    mQVec.push_back((stu2));

    mQVec.push_back((stu3));



    qSort(mQVec.begin(), mQVec.end(), comQt);

    QVector::iterator it1;

    for(it1 = mQVec.begin(); it1 != mQVec.end(); it1 ++)
    {
        stuInfo stu = *it1;
        cout << stu.name << endl;
    }


 /****************qt qsort vector 排序 End ***********************/




    return a.exec();
}

你可能感兴趣的:(c++)