C++中的結(jié)構(gòu)體vector排序問(wèn)題
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)過(guò)一個(gè)string作為中間的轉(zhuǎn)換變量,最終轉(zhuǎn)成int型,另外,我之前認(rèn)為由于是char型的原因,應(yīng)該主能傳遞0-255的參數(shù),但是仔細(xì)想一下是不對(duì)的,因?yàn)闊o(wú)論是多大的數(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)文章
c++中l(wèi)og4cplus日志庫(kù)使用的基本步驟和示例代碼
這篇文章主要給大家介紹了關(guān)于c++中l(wèi)og4cplus日志庫(kù)使用的相關(guān)資料,log4cplus是一款開源的c++日志庫(kù),具有線程安全,靈活,以及多粒度控制的特點(diǎn),log4cplus可以將日志按照優(yōu)先級(jí)進(jìn)行劃分,使其可以面向程序的調(diào)試,運(yùn)行,測(cè)試,后期維護(hù)等軟件全生命周期,需要的朋友可以參考下2024-06-06
C++實(shí)現(xiàn)無(wú)重復(fù)字符的最長(zhǎng)子串
本文主要介紹了C++實(shí)現(xiàn)無(wú)重復(fù)字符的最長(zhǎng)子串,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C/C++ Zlib庫(kù)封裝MyZip壓縮類的詳細(xì)過(guò)程
在軟件開發(fā)中,文件的壓縮和解壓縮是一項(xiàng)常見(jiàn)的任務(wù),而ZIP是一種被廣泛應(yīng)用的壓縮格式,本文將聚焦于一個(gè)簡(jiǎn)化的C++實(shí)現(xiàn),通過(guò)分析代碼,我們將深入了解其設(shè)計(jì)和實(shí)現(xiàn)細(xì)節(jié),感興趣的朋友一起看看吧2023-11-11

