随笔-4  评论-0  文章-1  trackbacks-0

std::sort多关键字排序
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct MultiData
{
    int a;
    int b;
   int c;
};

struct ICmpMuls
{
    bool operator()(const MultiData& first, const MultiData& second) const
    {
        if(first.a< second.a)return true;
        if(first.a== second.a)
           if(first.b< second.b)return true;
              if(first.b== second.b)
                 if(first.c< second.c)return true;
        return false;
    }
};

int main(int argc, char* argv[])
{
    vector<MultiData> vec_muls;
    for (int i = 0; i < 300; ++i)
        vec_muls.push_back(MultiData(rand()%20, rand()));
    sort(vec_muls.begin(), vec_muls.end(), ICmpMuls());
    for (size_t s = 0; s < vec_muls.size(); ++s)
        cout<<s<<":     "<<vec_muls[s].login<<" / "<<vec_muls[s].order<<endl;
    return 0;
}


posted on 2011-07-25 16:08 扬州炒饭 阅读(844) 评论(0)  编辑 收藏 引用 所属分类: STL源码剖析笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理