八千里路云和月

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  62 随笔 :: 0 文章 :: 0 评论 :: 37755 阅读

排序操作

c++ algorithm sort() 函数

该函数可以对 vector 进行排序,但是只保存排序后结果,没有索引。

 

root TMath::Sort() 函数

该函数可以有排序后索引。具体的使用方法:

复制代码
 1 void test()
 2 {
 3   Double_t t[4] = {10., 30., 20., 40.};
 4   Int_t id[4];
 5 
 6   TMath::Sort(4, t, id, kFALSE);
 7   for(int i=0;i<4;i++){
 8     cout << t[i] << endl;
 9   }
10 
11   for(int i=0;i<4;i++){
12     cout << id[i] << endl;
13   }
14 
15   return;
16 }
复制代码

 

map 容器

在 map 中,天然是按照 key 值排序的,不需要额外操作,举例如下:

复制代码
 1 void test2()
 2 {
 3   Double_t t[4] = {10., 30., 20., 40.};
 4   Double_t tv[4] = {100., 300., 200., 400.};
 5   Int_t id[4];
 6 
 7   map<double, double> map_ttv;
 8   for(int i=0;i<4;i++){
 9     map_ttv.insert(pair<double,double>(t[i], tv[i]));
10   }
11 
12   map<double, double>::iterator it;
13   for(it=map_ttv.begin();it!=map_ttv.end();++it){
14     cout << it->first << " " << it->second << endl;
15   }
16 
17 
18   return;
19 }
复制代码

 

posted on   hanX3  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示