对包含Struct的Vector就其中的一种属性排序(STL的强大)

上一篇 / 下一篇  2008-09-06 06:32:20 / 个人分类:vc vector

对包含Struct的Vector就其中的一种属性排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct StructTerm
{
    int TermID;
    float Occurency;
};

typedef vector<StructTerm> TERMS;
class Comparer
{
public:

    bool operator () (const StructTerm & va1, const StructTerm & va2)
    {
        return va1.Occurency > va2.Occurency;//有大到小排序
    }
};
int main()
{
    TERMS terms;
    StructTerm s;
    s.TermID = 1;
    s.Occurency = 0.8;
    terms.push_back(s);
    s.TermID = 2;
    s.Occurency = 0.6;
    terms.push_back(s);
    
    s.TermID = 2000;
    s.Occurency = 0.68;
    terms.push_back(s);
   
    sort(terms.begin(), terms.end(), Comparer());

    for(int i = 0; i < terms.size(); ++i)
    {
        cout << terms[i].TermID << "  ";
    }
    cout << endl;
}

>>1 2000 2

TAG: Vector STL Struct 属性

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-11-22  
      1
2345678
9101112131415
16171819202122
23242526272829
30      

数据统计

  • 访问量: 62
  • 日志数: 2
  • 图片数: 1
  • 建立时间: 2008-08-22
  • 更新时间: 2008-09-07

RSS订阅

Open Toolbar