[英]Range of the first element of a vector pair
I need to find the range of the first elements of a vector pair. 我需要找到向量对的第一个元素的范围。 I need this range for a map, which counts the duplicate entries in this vector.
我需要此范围的地图,该地图计算此向量中的重复条目。 Here is a code snipped and how I managed it.
这是一段代码片段,以及如何管理它。 Maybe there is another, better solution?
也许还有另一个更好的解决方案?
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";
}
}
}
So is there a possibility to remove Part 2 and find out the range of the first Element of Roi_Num_Koord
?, so that I don't have to copy the first elements of this vector to the other vector ( Roi_first_Element
) 所以有可能删除第2部分并找出
Roi_Num_Koord
的第一个Element的Roi_Num_Koord
,这样我就不必将此向量的前一个元素复制到另一个向量( Roi_first_Element
)
Yes the second step is completely redundant. 是的,第二步是完全多余的。 You just iterate through the container and whenever you need first element of the pair you say it explicitly pretty much like you do in Step 2.
您只需要遍历该容器,每当需要该对中的第一个元素时,就可以像在第2步中所做的那样显式地说出来。
for(const pair<unsigned int,Point>& element : Roi_Num_Koord)
{
++frequency[element.first];
cout << "freque " << frequency[element.first] << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.