我需要找到向量对的第一个元素的范围。我需要这个范围的地图,计数重复的条目在这个向量。下面是一个代码片段,以及我如何管理它。也许还有另一个更好的解决方案?
unordered_map<int, int> frequency;
vector<pair<unsigned int,Point>> Roi_Num_Koord;
vector<int> Roi_first_Element;
int main()
{
// Part1: fill the Vector pair
Roi_Num_Koord.emplace_back(make_pair(0,Point(3.6));
Roi_Num_Koord.emplace_back(make_pair(1,Point(4,8));
Roi_Num_Koord.emplace_back(make_pair(2,Point(8.3));
Roi_Num_Koord.emplace_back(make_pair(3,Point(4,6));
// Part 2: now copy the first element to another vector
for (int i = 0; i < Roi_Num_Koord.size(); i++)
{
Roi_first_Element.emplace_back(Roi_Num_Koord[i].first);
}
// Part 3: now do the duplicate search (Code was taken out of the internet)
for (int i : Roi_first_Element)
{
++frequency[i];
cout << "freque "<<frequency[i] << endl;
}
for (const auto& e : frequency)
{
if (e.second == 5)
{
std::cout << "Roi " << e.first << " encountered " << e.second << " times\n";
}
}
}那么,是否有可能删除第2部分并找出Roi_Num_Koord的第一个元素的范围?这样,我就不必将该向量的第一个元素复制到另一个向量(Roi_first_Element)。
发布于 2018-06-13 07:47:08
是的,第二步是完全多余的。您只需遍历容器,每当需要这对元素的第一个元素时,就会显式地表示它,就像在步骤2中所做的那样。
for(const pair<unsigned int,Point>& element : Roi_Num_Koord)
{
++frequency[element.first];
cout << "freque " << frequency[element.first] << endl;
}https://stackoverflow.com/questions/50831547
复制相似问题