#include <QCoreApplication>
#include <vector>
#include <string>
#include <iostream>
#include <QVector>
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<stuInfo> mVec;
mVec.push_back((stu0));
mVec.push_back((stu1));
mVec.push_back((stu2));
mVec.push_back((stu3));
vector<stuInfo>::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<stuInfo> mQVec;
mQVec.push_back((stu0));
mQVec.push_back((stu1));
mQVec.push_back((stu2));
mQVec.push_back((stu3));
qSort(mQVec.begin(), mQVec.end(), comQt);
QVector<stuInfo>::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++ 排序一个vector 和 QVector 排序操作
最新推荐文章于 2024-07-27 23:32:27 发布