C++中的結(jié)構(gòu)體vector排序問題
C++結(jié)構(gòu)體vector排序
使用sort函數(shù)對一個vector很常用,前提是通文件中必須包含#include ,但是針對結(jié)構(gòu)體vector排序則需要進行一定的改動。
具體事例如下所示
// sort algorithm example #include <iostream> ? ? // std::cout #include <algorithm> ? ?// std::sort #include <vector> ? ? ? // std::vector bool myfunction (int i,int j) { return (i<j); } struct myclass { ? bool operator() (int i,int j) { return (i<j);} } myobject; int main () { ? int myints[] = {32,71,12,45,26,80,53,33}; ? std::vector<int> myvector (myints, myints+8); ? ? ? ? ? ? ? // 32 71 12 45 26 80 53 33 ? // using default comparison (operator <): ? std::sort (myvector.begin(), myvector.begin()+4); ? ? ? ? ? //(12 32 45 71)26 80 53 33 ? // using function as comp ? std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) ? // using object as comp ? std::sort (myvector.begin(), myvector.end(), myobject); ? ? //(12 26 32 33 45 53 71 80) ? // print out content: ? std::cout << "myvector contains:"; ? for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) ? ? std::cout << ' ' << *it; ? std::cout << '\n'; ? return 0; }
但是當(dāng)vector中的變量是結(jié)構(gòu)體,并且需要按照結(jié)構(gòu)體的某一個元素進行排序時,則需要進行一定的修改:
#include "privateHeader.h" #include <string> #include <vector> #include <iostream> #include <algorithm> using std::string; using std::vector; using std::cout; using std::endl; using namespace std; typedef struct { ? ? float score; ? ? string file_name; ? ? string all_file_name; }TFileProp; bool GreaterSort(TFileProp a, TFileProp b) { ? ? return (a.score > b.score); } bool LessSort(TFileProp a, TFileProp b) { ? ? return (a.score < b.score); } vector<TFileProp> VecFileProp; VecFileProp.push_back(tFileProp); ? ?//對vector進行push操作 std::sort(VecFileProp.begin(), VecFileProp.end(), GreaterSort); ? ?//進行降序排序 std::sort(VecFileProp.begin(), VecFileProp.end(), LessSort); ? ?//進行升序排序
還有一點,利用Iang傳遞參一個數(shù)據(jù)時,由于命令行接收的參數(shù)是以char** argv存儲的,因此需要先進行強制類型轉(zhuǎn)換,經(jīng)過一個string作為中間的轉(zhuǎn)換變量,最終轉(zhuǎn)成int型,另外,我之前認為由于是char型的原因,應(yīng)該主能傳遞0-255的參數(shù),但是仔細想一下是不對的,因為無論是多大的數(shù),都是以一個字符串傳遞進去的,然后string類型再進行強轉(zhuǎn)的時候就轉(zhuǎn)陳了int型,因此并不存在256的大小限制。
int main(int argc, char** argv) { ? ? // 統(tǒng)計時間 ? ? //timeStatistics(); ? ? // 所有結(jié)果放到一個文件夾顯示 ? ? int num_save; ? ? if (argc == 2) ? ? { ? ? ? ? std::string thres = argv[1]; ? ? ? ? num_save = atof(thres.c_str()); ? ? ? ? //std::cout << "(int)argv[1] is " << argv[1]; ? ? ? ? //std::cout << "num_save is " << num_save; ? ? } ? ? else ? ? { ? ? ? ? num_save = 100; ? ? } ? ? showAllResult(num_save); ? ? return 1; }
自定義結(jié)構(gòu)體vector排序
寫給自己
可以使用結(jié)構(gòu)體內(nèi)重載小于號,也可以使用編寫cmp函數(shù)的形式
#include<iostream> #include<vector> #include<algorithm> using namespace std; typedef struct stu{ int niD; string strName; bool operator < (stu const& a) const { if(niD < a.niD) return true; if(niD == a.niD) return strName.compare(a.strName) < 0; return false; } }stu; bool cmp(stu a,stu b){ if(a.niD > b.niD) return true; if(a.niD == b.niD) return a.strName.compare(b.strName) < 0; return false; } int main(){ vector<stu> v; stu a; stu b; a.niD=1; a.strName="sfr"; b.niD=0; b.strName="sfr1"; v.push_back(a); v.push_back(b); for(stu s:v) cout<<s.niD<<endl; //結(jié)構(gòu)體內(nèi)重載小于號 sort(v.begin(),v.end()); for(stu s:v) cout<<s.niD<<endl; //使用cmp函數(shù) sort(v.begin(),v.end(),cmp); for(stu s:v) cout<<s.niD<<endl; return 0; }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實現(xiàn)帶頭結(jié)點的鏈表的創(chuàng)建、查找、插入、刪除操作
這篇文章主要介紹了C語言實現(xiàn)帶頭結(jié)點的鏈表的創(chuàng)建、查找、插入、刪除操作方法,對于了解數(shù)據(jù)結(jié)構(gòu)中鏈表的各項操作有很好的借鑒價值,需要的朋友可以參考下2014-09-09