对 pair 和 double 的映射进行排序
Sorting a map of pair and double
我有一张地图:map< pair < int , int > , long double> valore
。这对代表我的坐标系和 (i,j) 坐标中值的两倍。
现在我必须将这张地图从小到大排序(显然坐标必须链接到相应的双精度)。有人可以帮助我吗?
您只需编写一个自定义比较器即可。在这里您必须构建一个完整的对象,因为您需要根据特定映射中的值来比较键。这应该符合您的要求:
class Comparator {
std::map<std::pair<int, int>, double>& orig_map;
public:
Comparator(std::map<std::pair<int, int>, double>& orig_map)
: orig_map(orig_map) {}
bool operator () (const std::pair<int, int>& first,
const std::pair<int, int>& second) const {
return orig_map[first] < orig_map[second];
}
};
您可以使用它从原始地图构建特别定制的地图:
std::map< pair < int , int > , long double> valore;
// load the map valore ...
// build a copy of valore sorted according to its value
Comparator comp(map);
std::map<std::pair<int, int>, double, Comparator> val_sorted(valore.begin(),
valore.end(), comp);
您可以迭代 val_sorted
,它按其值排序
注意:永远不要在 val_sorted
中插入 valore
中不存在的元素。正确的使用方法是每次原始地图发生变化时创建一个新实例,或者至少将其清空并重新加载。
正如其他人所提到的,无法直接使用这些值对地图进行排序。
使用值对地图进行排序的一种方法如下。
- 创建一个向量,其中包含一对地图元素。即,
pair<Value, Key>
。在您的情况下,这将是 vector< pair<double, pair<int, int> > >
- 对向量进行排序(使用自定义排序函数),现在您的元素已按值排序。
请看下面的例子。 (使用 -std=c++11
选项编译)
#include <bits/stdc++.h>
using namespace std;
/* custom compare function. ascending order. Compares first elemens of p1 & p2 */
static bool custom_compare(const pair< double, pair<int, int> > & p1, const pair<double, pair<int, int> > & p2) {
return p1.first < p2.first;
}
void sortfn(map<pair<int, int>, double>& m) {
/* vector if pairs to hold values. */
vector< pair<double, pair<int, int> > > vec;
/* traverse the map and populate the vector */
for (auto it = m.begin(); it != m.end(); it++) {
vec.push_back(make_pair(it->second, it->first));
}
/* call sort method on the vector with custom compare method */
sort(vec.begin(), vec.end(), custom_compare);
for (auto it = vec.begin(); it != vec.end(); it++) {
cout<<it->first<<" ( "<<it->second.first<<", "<<it->second.second<<" )"<<endl;
}
}
int main() {
map<pair<int, int>, double> m;
m.insert(make_pair(make_pair(0, 0), 5));
m.insert(make_pair(make_pair(1, 1), 0));
m.insert(make_pair(make_pair(2, 2), 10));
sortfn(m);
return 0;
}
输出
0 ( 1, 1 )
5 ( 0, 0 )
10 ( 2, 2 )
由于下图包含第一个参数作为整数对,第二个参数作为双精度参数,因此无法根据第一个参数(整数对)进行排序。
map< pair < int , int > , long double> valore
需要编写一个自定义函数,用于根据坐标进行排序。
我有一张地图:map< pair < int , int > , long double> valore
。这对代表我的坐标系和 (i,j) 坐标中值的两倍。
现在我必须将这张地图从小到大排序(显然坐标必须链接到相应的双精度)。有人可以帮助我吗?
您只需编写一个自定义比较器即可。在这里您必须构建一个完整的对象,因为您需要根据特定映射中的值来比较键。这应该符合您的要求:
class Comparator {
std::map<std::pair<int, int>, double>& orig_map;
public:
Comparator(std::map<std::pair<int, int>, double>& orig_map)
: orig_map(orig_map) {}
bool operator () (const std::pair<int, int>& first,
const std::pair<int, int>& second) const {
return orig_map[first] < orig_map[second];
}
};
您可以使用它从原始地图构建特别定制的地图:
std::map< pair < int , int > , long double> valore;
// load the map valore ...
// build a copy of valore sorted according to its value
Comparator comp(map);
std::map<std::pair<int, int>, double, Comparator> val_sorted(valore.begin(),
valore.end(), comp);
您可以迭代 val_sorted
,它按其值排序
注意:永远不要在 val_sorted
中插入 valore
中不存在的元素。正确的使用方法是每次原始地图发生变化时创建一个新实例,或者至少将其清空并重新加载。
正如其他人所提到的,无法直接使用这些值对地图进行排序。 使用值对地图进行排序的一种方法如下。
- 创建一个向量,其中包含一对地图元素。即,
pair<Value, Key>
。在您的情况下,这将是vector< pair<double, pair<int, int> > >
- 对向量进行排序(使用自定义排序函数),现在您的元素已按值排序。
请看下面的例子。 (使用 -std=c++11
选项编译)
#include <bits/stdc++.h>
using namespace std;
/* custom compare function. ascending order. Compares first elemens of p1 & p2 */
static bool custom_compare(const pair< double, pair<int, int> > & p1, const pair<double, pair<int, int> > & p2) {
return p1.first < p2.first;
}
void sortfn(map<pair<int, int>, double>& m) {
/* vector if pairs to hold values. */
vector< pair<double, pair<int, int> > > vec;
/* traverse the map and populate the vector */
for (auto it = m.begin(); it != m.end(); it++) {
vec.push_back(make_pair(it->second, it->first));
}
/* call sort method on the vector with custom compare method */
sort(vec.begin(), vec.end(), custom_compare);
for (auto it = vec.begin(); it != vec.end(); it++) {
cout<<it->first<<" ( "<<it->second.first<<", "<<it->second.second<<" )"<<endl;
}
}
int main() {
map<pair<int, int>, double> m;
m.insert(make_pair(make_pair(0, 0), 5));
m.insert(make_pair(make_pair(1, 1), 0));
m.insert(make_pair(make_pair(2, 2), 10));
sortfn(m);
return 0;
}
输出
0 ( 1, 1 )
5 ( 0, 0 )
10 ( 2, 2 )
由于下图包含第一个参数作为整数对,第二个参数作为双精度参数,因此无法根据第一个参数(整数对)进行排序。
map< pair < int , int > , long double> valore
需要编写一个自定义函数,用于根据坐标进行排序。