網站首頁 編程語言 正文
C++結構體vector排序
使用sort函數對一個vector很常用,前提是通文件中必須包含#include ,但是針對結構體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;
}
但是當vector中的變量是結構體,并且需要按照結構體的某一個元素進行排序時,則需要進行一定的修改:
#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傳遞參一個數據時,由于命令行接收的參數是以char** argv存儲的,因此需要先進行強制類型轉換,經過一個string作為中間的轉換變量,最終轉成int型,另外,我之前認為由于是char型的原因,應該主能傳遞0-255的參數,但是仔細想一下是不對的,因為無論是多大的數,都是以一個字符串傳遞進去的,然后string類型再進行強轉的時候就轉陳了int型,因此并不存在256的大小限制。
int main(int argc, char** argv)
{
? ? // 統計時間
? ? //timeStatistics();
? ? // 所有結果放到一個文件夾顯示
? ? 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;
}
自定義結構體vector排序
寫給自己
可以使用結構體內重載小于號,也可以使用編寫cmp函數的形式
#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;
//結構體內重載小于號
sort(v.begin(),v.end());
for(stu s:v)
cout<<s.niD<<endl;
//使用cmp函數
sort(v.begin(),v.end(),cmp);
for(stu s:v)
cout<<s.niD<<endl;
return 0;
}
原文鏈接:https://blog.csdn.net/guzhao9901/article/details/107336220
相關推薦
- 2022-04-05 svn使用命令忽略指定目錄 svn propset svn:ignore “要忽略的目錄“ .
- 2022-10-21 C++調用matlab函數的實例_C 語言
- 2022-08-17 go?Cobra命令行工具入門教程_Golang
- 2022-10-08 Golang?Web?框架Iris安裝部署_Golang
- 2022-02-01 在linux環境下com.aspose.words將word文件轉為pdf后亂碼,window環境下
- 2022-10-14 【Python】pytorch 保存模型、checkpoint
- 2022-04-28 C++Stack棧類模版實例詳解_C 語言
- 2022-07-08 Pytest如何使用mark的方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支