zoukankan      html  css  js  c++  java
  • C++中的结构体vector排序

    在包含了头文件#include <algorithm>之后,就可以直接利用sort函数对一个vector进行排序了:

     1 // sort algorithm example  
     2 #include <iostream>     // std::cout  
     3 #include <algorithm>    // std::sort  
     4 #include <vector>       // std::vector  
     5   
     6 bool myfunction (int i,int j) { return (i<j); }  
     7   
     8 struct myclass {  
     9   bool operator() (int i,int j) { return (i<j);}  
    10 } myobject;  
    11   
    12 int main () {  
    13   int myints[] = {32,71,12,45,26,80,53,33};  
    14   std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33  
    15   
    16   // using default comparison (operator <):  
    17   std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33  
    18   
    19   // using function as comp  
    20   std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)  
    21   
    22   // using object as comp  
    23   std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)  
    24   
    25   // print out content:  
    26   std::cout << "myvector contains:";  
    27   for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)  
    28     std::cout << ' ' << *it;  
    29   std::cout << '
    ';  
    30   
    31   return 0;  
    32 }  

    但是当vector中的变量是结构体,并且需要按照结构体的某一个元素进行排序时,则需要进行一定的修改:

     1 #include "privateHeader.h"
     2 #include <string>
     3 #include <vector>
     4 #include <iostream>
     5 #include <algorithm>
     6 using std::string;
     7 using std::vector;
     8 using std::cout;
     9 using std::endl;
    10 using namespace std;
    11 
    12 typedef struct
    13 {
    14     float score;
    15     string file_name;
    16     string all_file_name;
    17 
    18 }TFileProp;
    19 
    20 bool GreaterSort(TFileProp a, TFileProp b)
    21 { 
    22     return (a.score > b.score);
    23 }
    24 bool LessSort(TFileProp a, TFileProp b)
    25 { 
    26     return (a.score < b.score);
    27 }
    28 vector<TFileProp> VecFileProp;
    29 
    30 VecFileProp.push_back(tFileProp);    //对vector进行push操作
    31 
    32 std::sort(VecFileProp.begin(), VecFileProp.end(), GreaterSort);    //进行降序排序
    33 std::sort(VecFileProp.begin(), VecFileProp.end(), LessSort);    //进行升序排序

    还有一点,利用Iang传递参一个数据时,由于命令行接收的参数是以char** argv存储的,因此需要先进行强制类型转换,经过一个string作为中间的转换变量,最终转成int型,另外,我之前认为由于是char型的原因,应该主能传递0-255的参数,但是仔细想一下是不对的,因为无论是多大的数,都是以一个字符串传递进去的,然后string类型再进行强转的时候就转陈了int型,因此并不存在256的大小限制。

     1 int main(int argc, char** argv)
     2 {
     3     // 统计时间
     4     //timeStatistics();
     5 
     6     // 所有结果放到一个文件夹显示
     7     
     8     int num_save;
     9     if (argc == 2)
    10     {
    11         std::string thres = argv[1];
    12         num_save = atof(thres.c_str());
    13         //std::cout << "(int)argv[1] is " << argv[1];
    14         //std::cout << "num_save is " << num_save;
    15     }
    16     else
    17     {
    18         num_save = 100;
    19     }
    20     showAllResult(num_save);
    21     
    22     
    23     return 1;
    24 }

    参考:http://blog.csdn.net/zhouxun623/article/details/49887555

  • 相关阅读:
    MFC CListCtrl 使用介绍
    头文件预编译 .
    WM_CREATE和WM_INITDIALOG
    一步一步学List Control控件的用法(第三步)设置风格 .
    VC定时器的用法:SetTimer和Ontimer .
    GetWindowRect与GetClientRect 的区别 .
    Prebuild Command line(Copy) . vc2008预生成事件和生成后事件的用法
    MFC浅析(7) CWnd类虚函数的调用时机、缺省实现 .
    MFC中VALUE和CONTROL的区别(EDIT控件)
    MFC应用程序中处理消息的顺序
  • 原文地址:https://www.cnblogs.com/rainsoul/p/6290854.html
Copyright © 2011-2022 走看看