C++中的結(jié)構(gòu)體vector排序問題
C++結(jié)構(gòu)體vector排序
使用sort函數(shù)對(duì)一個(gè)vector很常用,前提是通文件中必須包含#include ,但是針對(duì)結(jié)構(gòu)體vector排序則需要進(jìn)行一定的改動(dòng)。
具體事例如下所示
// 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)體的某一個(gè)元素進(jìn)行排序時(shí),則需要進(jìn)行一定的修改:
#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); ? ?//對(duì)vector進(jìn)行push操作 std::sort(VecFileProp.begin(), VecFileProp.end(), GreaterSort); ? ?//進(jìn)行降序排序 std::sort(VecFileProp.begin(), VecFileProp.end(), LessSort); ? ?//進(jìn)行升序排序
還有一點(diǎn),利用Iang傳遞參一個(gè)數(shù)據(jù)時(shí),由于命令行接收的參數(shù)是以char** argv存儲(chǔ)的,因此需要先進(jìn)行強(qiáng)制類型轉(zhuǎn)換,經(jīng)過一個(gè)string作為中間的轉(zhuǎn)換變量,最終轉(zhuǎn)成int型,另外,我之前認(rèn)為由于是char型的原因,應(yīng)該主能傳遞0-255的參數(shù),但是仔細(xì)想一下是不對(duì)的,因?yàn)闊o論是多大的數(shù),都是以一個(gè)字符串傳遞進(jìn)去的,然后string類型再進(jìn)行強(qiáng)轉(zhuǎn)的時(shí)候就轉(zhuǎn)陳了int型,因此并不存在256的大小限制。
int main(int argc, char** argv) { ? ? // 統(tǒng)計(jì)時(shí)間 ? ? //timeStatistics(); ? ? // 所有結(jié)果放到一個(gè)文件夾顯示 ? ? 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)重載小于號(hào),也可以使用編寫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)重載小于號(hào) 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; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Qt編寫地圖實(shí)現(xiàn)省市區(qū)域圖的示例代碼
本文主要介紹了Qt編寫地圖實(shí)現(xiàn)省市區(qū)域圖的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12C語言實(shí)現(xiàn)帶頭結(jié)點(diǎn)的鏈表的創(chuàng)建、查找、插入、刪除操作
這篇文章主要介紹了C語言實(shí)現(xiàn)帶頭結(jié)點(diǎn)的鏈表的創(chuàng)建、查找、插入、刪除操作方法,對(duì)于了解數(shù)據(jù)結(jié)構(gòu)中鏈表的各項(xiàng)操作有很好的借鑒價(jià)值,需要的朋友可以參考下2014-09-09OpenCV使用BSM統(tǒng)計(jì)視頻中移動(dòng)的對(duì)象
這篇文章主要為大家詳細(xì)介紹了OpenCV如何使用BackgroundSubstractor(BSM)實(shí)現(xiàn)視頻中移動(dòng)對(duì)象統(tǒng)計(jì)功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-02-02