当第一个密钥更改时,将对持续的映射保持持续
Get the size of a map of pairs continous when the first key changes
我有一个对 map<pair<int, int>, int> counts
的映射。我正在迭代此处,并且我正在计算一些浮点数,如果地图的值大于2。我的一对。我地图中值的示例:
1 1 4
1 2 5
1 3 5
1 7 5
1 29 2 ..现在的第一个键更改,因此大小为4(不是5,因为一个值是&lt; 3(
2 10 1
2 20 4 ...
我试图以最小的示例来解释它:
int main(int argc, char** argv)
{
// Create some values - both vectors have the same size
vector<pair<int, int>> pairs;
pairs.push_back(make_pair(1, 1));
pairs.push_back(make_pair(1, 2));
pairs.push_back(make_pair(1, 4));
pairs.push_back(make_pair(2, 7));
pairs.push_back(make_pair(2, 4));
pairs.push_back(make_pair(3, 5));
pairs.push_back(make_pair(3, 7));
pairs.push_back(make_pair(3, 8));
vector<float> value;
value.push_back(4);
value.push_back(5);
value.push_back(8);
value.push_back(2);
value.push_back(5);
value.push_back(6);
value.push_back(7);
value.push_back(8);
map<pair<int, int>, int> counts;
vector<vector<vector<float>>> descriptors_1, descriptors_2;
vector<float> oa_simil;
float Overall_Similarity;
for (size_t i = 0; i < pairs.size(); i++) {
counts.insert(make_pair(make_pair(pairs[i].first, pairs[i].second), value[i]));
}
for (const auto& p : counts) {
const auto& p1 = p.first.first;
const auto& p2 = p.first.second;
int count = p.second;
if (p.second >= 3) {
float S = 0;
// Two for-loops that calculate a new S for every p1, p2 combination >= 3
//S = ls_1;
// 5 Linesegments in image 1 are compared to 5 Linesegments in image 2, if p.second >= 3
for (int ls_1 = 0; ls_1 < 5; ls_1++) {
for (int ls_2 = 0; ls_2 < 5; ls_2++) {
pair<int, int> index_1, index_2;
index_1 = make_pair(p1, ls_1);
index_2 = make_pair(p2, ls_2);
// Calculate the Similarity of different pairs of line segments in a complex function
calculateSimilarity(Overall_Similarity, descriptors_1, descriptors_2, index_1, index_2);
oa_simil.push_back(Overall_Similarity);
}
// Get the maximum similarity of every single line segment in one image
float max_M = *max_element(oa_simil.begin(), oa_simil.end());
oa_simil.clear();
// Sum up the maxima -> S contains the maximum for the combination of two Linesegments p1, p2 (if p >=3)
S += max_M;
}
// Now I want to get the maximum value S for every single p1
// In the example I want the maximum S out of e.g. the 3 pairs (1,1), (1,2), (1,4).
// Trying to push the S in a vector<float> when p1 changes.
}
}
}
我已经试图将我的所有键和S的价值纳入新地图,但是当我已经通过键迭代时,这似乎是浪费计算时间。
我想我解决了它。我为我的成对和S值创建了一张地图,并逆转了结果,以使其成为可能最大的价值。然后,我将我的P1与上一个迭代的P1进行了测试,以测试该值是否更改。
template<typename A, typename B>
std::pair<B, A> flip_pair(const std::pair<A, B> &p)
{
return std::pair<B, A>(p.second, p.first);
}
template<typename A, typename B>
std::multimap<B, A> flip_map(const std::map<A, B> &src)
{
std::multimap<B, A> dst;
std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
flip_pair<A, B>);
return dst;
}
int main(int argc, char** argv)
{
map<pair<int, int>, float> similarities;
multimap<float, pair<int, int>> inversed_map;
...
// Get the maximum similarity of every single line segment in one image
float max_M = *max_element(oa_simil.begin(), oa_simil.end());
oa_simil.clear();
// Sum up the maxima -> S contains the maximum for the combination of two Linesegments p1, p2 (if p >=3)
S += max_M;
}
similarities.insert(make_pair(make_pair(p1, p2), S));
inversed_map = flip_map(similarities);
if ((p1 > p1_old) && ((int)inversed_map.size() > 0)) {
// Sort the vector and keep track of the indexes of the pair.
auto it = inversed_map.rbegin();
cout << it->second.first << " " << it->second.second << " " << it->first << endl;
similarities.clear();
inversed_map.clear();
}
p1_old = p1;
}
}
}
相关文章:
- 重载std::映射不同的密钥类型
- 如何在嵌套映射(C++)中高效地查找密钥
- C++密钥和文件的映射不起作用
- 当第一个密钥更改时,将对持续的映射保持持续
- 根据类成员对同一密钥的多映射元素进行排序
- C 11多态映射键静态断言失败:哈希函数必须与密钥类型的参数无关
- 如何使用密钥 2 更新 STL 映射键 1 并将值复制到键 2 中
- 相同的密钥(使用malloc创建)添加了两次映射
- 擦除C++映射中包含单个值的密钥
- 如何在c++中跨不同的映射共享密钥
- C++STL映射无法识别密钥
- 如何避免在基于 B 树的类 STL 映射中浪费地复制密钥
- 检查是否在映射 C++ 中找到并获取密钥
- 如何使用 C++11 中的函数式编程从映射中获取密钥
- 避免重复存储映射密钥
- 具有自定义密钥类型的STL无序映射
- 在一对多映射中搜索一个密钥
- 唯一密钥和哈希的无序映射
- 我在多映射中得到了两次相同的密钥,它只存在于文本文件中
- 映射v.s.unordered_map中可允许的密钥类型