#include "stdafx.h"
#include <vector>
#include <algorithm>
using namespace std;
struct SPxSampleItem
{
int nIndex;
int nValue;
SPxSampleItem()
{
Flush();
}
~SPxSampleItem()
{
Flush();
}
void Flush()
{
nIndex = -1;
nValue = -1;
}
};
// 升序排序规则
inline bool index_ascending(const SPxSampleItem & m1, const SPxSampleItem & m2)
{
return m1.nIndex < m2.nIndex;
}
// 降序排序规则
inline bool value_descending(const SPxSampleItem & m1, const SPxSampleItem & m2)
{
return m1.nValue > m2.nValue;
}
int main( void)
{
vector <SPxSampleItem> vsItem;
SPxSampleItem sSampleItem;
sSampleItem.nIndex = 3;
sSampleItem.nValue = 500;
vsItem.push_back(sSampleItem);
sSampleItem.nIndex = 2;
sSampleItem.nValue = 5;
vsItem.push_back(sSampleItem);
sSampleItem.nIndex = 1;
sSampleItem.nValue = 5000;
vsItem.push_back(sSampleItem);
sSampleItem.nIndex = 4;
sSampleItem.nValue = 50;
vsItem.push_back(sSampleItem);
printf("\n The original data ...\n");
vector <SPxSampleItem> ::iterator iter = vsItem.begin();
for(; iter != vsItem.end(); ++iter)
{
printf("Index: %4d, Value: %4d\n", iter->nIndex, iter->nValue);
}
// 根据value按降序排列
::sort(vsItem.begin(), vsItem.end(), value_descending);
printf("\nAfter descending by value ...\n");
iter = vsItem.begin();
for(; iter != vsItem.end(); ++iter)
{
printf("Index: %4d, Value: %4d\n", iter->nIndex, iter->nValue);
}
// 根据index进行升序排列
::sort(vsItem.begin(), vsItem.end(), index_ascending);
printf("\nAfter ascending by index ...\n");
iter = vsItem.begin();
for(; iter != vsItem.end(); ++iter)
{
printf("Index: %4d, Value: %4d\n", iter->nIndex, iter->nValue);
}
getchar();
return 0;
}
运行结果: