網(wǎng)站首頁 編程語言 正文
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型,另外,我之前認(rèn)為由于是char型的原因,應(yīng)該主能傳遞0-255的參數(shù),但是仔細(xì)想一下是不對的,因為無論是多大的數(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;
}
原文鏈接:https://blog.csdn.net/guzhao9901/article/details/107336220
相關(guān)推薦
- 2022-10-02 Python+OpenCV讀寫視頻的方法詳解_python
- 2022-05-17 C++?std::shared_mutex讀寫鎖的使用_C 語言
- 2022-07-22 C語言中字符串詳解
- 2022-04-22 redis 為什么對數(shù)字/字符串a(chǎn)ppend操作后,編碼格式object encoding從int/
- 2022-10-08 docker安裝redis并掛載到本地的詳細(xì)教程_docker
- 2022-02-25 C++實現(xiàn)單例模式的方法_C 語言
- 2023-12-15 IDEA去掉activate-power-mode右上角圖標(biāo)和Power Mode II 進度條10
- 2022-10-15 Go?Excelize?API源碼解讀GetSheetViewOptions與SetPageLayo
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支