排序操作
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 }