Chinaunix首页 | 论坛 | 博客
  • 博客访问: 102697
  • 博文数量: 30
  • 博客积分: 305
  • 博客等级: 二等列兵
  • 技术积分: 320
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-09 12:31
文章分类

全部博文(30)

文章存档

2014年(3)

2013年(16)

2012年(11)

我的朋友

分类: C/C++

2012-11-28 10:16:36

vector容器的排序方法
1. 重载 "<",用于 vactor 排序
2. 重载 "()",用于 vector 排序
3. 提供一个比较函数,用于vector 排序
具体代码:

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <vector>
  3. /** vecor sort
  4.  ** 降序
  5.  **/
  6. using namespace std;
  7. class testIndex
  8. {
  9.     private:
  10.         int value;
  11.         int index;
  12.     public:
  13.         testIndex()
  14.         {
  15.             value = 0;
  16.             index = 0;
  17.         }
  18.         testIndex(int idx,int v)
  19.         {
  20.             value = v;
  21.             index = idx;
  22.         }
  23.         int getValue() const
  24.         {
  25.             return value;
  26.         }
  27.         int getIndex() const
  28.         {
  29.             return index;
  30.         }
  31.         bool operator ()(const testIndex *t1,const testIndex *t2) const
  32.         {
  33.             if(t1->value > t2 -> value)
  34.             {
  35.                 return true;
  36.             }
  37.             else if(t1->value < t2->value)
  38.             {
  39.                 return false;
  40.             }
  41.             if(t1->index > t2 ->index)
  42.             {
  43.                 return true;
  44.             }
  45.             else
  46.             {
  47.                 return false;
  48.             }
  49.         }
  50.         bool operator < (const testIndex &ti) const
  51.         {
  52.             if(value > ti.value)
  53.             {
  54.                 return true;
  55.             }
  56.             else if(value < ti.value)
  57.             {
  58.                 return false;
  59.             }
  60.             if(index > ti.index)
  61.             {
  62.                 return true;
  63.             }
  64.             else
  65.             {
  66.                 return false;
  67.             }
  68.         }
  69. };
  70. bool compare_index(const testIndex *t1,const testIndex *t2)
  71. {
  72.     if(t1->getValue() > t2->getValue())
  73.     {
  74.         return true;
  75.     }
  76.     else if(t1->getValue() < t2->getValue())
  77.     {
  78.         return false;
  79.     }
  80.     if(t1->getIndex() > t2->getIndex())
  81.     {
  82.         return true;
  83.     }
  84.     else
  85.     {
  86.         return false;
  87.     }

  88. }
  89. int main(int argc,char ** argv)
  90. {
  91.     vector<testIndex *> testVector1;
  92.     vector<testIndex > testVector2;
  93.     testIndex *t1 = new testIndex(0,10);
  94.     testIndex *t2 = new testIndex(1,11);
  95.     testIndex *t3 = new testIndex(2,12);
  96.     testIndex *t4 = new testIndex(3,12);
  97.     testIndex *t5 = new testIndex(4,12);
  98.     testIndex *t6 = new testIndex(5,15);
  99.     testVector1.push_back(t1);
  100.     testVector1.push_back(t2);
  101.     testVector1.push_back(t3);
  102.     testVector1.push_back(t4);
  103.     testVector1.push_back(t5);
  104.     testVector1.push_back(t6);
  105.     testVector2.push_back(*t1);
  106.     testVector2.push_back(*t2);
  107.     testVector2.push_back(*t3);
  108.     testVector2.push_back(*t4);
  109.     testVector2.push_back(*t5);
  110.     testVector2.push_back(*t6);
  111.     vector<testIndex *>::iterator it;
  112.     vector<testIndex > ::iterator it2;
  113.     sort(testVector2.begin(),testVector2.end()); //<< compare by "<"a
  114.     for(it2 = testVector2.begin();it2 != testVector2.end();it2++)
  115.     {
  116.         cout << (*it2).getValue() << "\t" << (*it2).getIndex() << endl;
  117.     }
  118.     cout << endl;
  119.     sort(testVector1.begin(),testVector1.end(),testIndex()); //<< compare by "()"
  120.     for(it = testVector1.begin();it != testVector1.end();it++)
  121.     {
  122.         cout << (*it)->getValue() << "\t" << (*it)->getIndex() << endl;
  123.     }
  124.     sort(testVector1.begin(),testVector1.end(),compare_index); //<< compare by "compare_index"
  125.     cout << endl;
  126.     for(it = testVector1.begin();it != testVector1.end();it++)
  127.     {
  128.         cout << (*it)->getValue() << "\t" << (*it)->getIndex() << endl;
  129.     }
  130.     return 0;
  131. }

阅读(5062) | 评论(0) | 转发(0) |
0

上一篇:tar 压缩文件

下一篇:c++ 按行读取 (getline)

给主人留下些什么吧!~~