简体   繁体   English

向量对的第一个元素的范围

[英]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.

相关问题 在向量上找到第一对元素(int)并获得第二对元素(struct) - find first pair element (int) on a vector and get second (struct) 如何访问向量中的元素 <map<first, pair<K,V> &gt;&gt; - How to access element in vector<map<first, pair<K,V>>> 从一维向量复制<int>从二维向量对向量的第一个元素开始<pair<int,int> &gt;矩阵 - Copying from one dimensional vector vector<int> starts to first element of two dimensional vector pair vector<pair<int,int>>matrix 如何将对向量中的对的第一个元素(字符串)与另一个字符串进行比较? - How do I compare the first element (string) of a pair in a pair vector with another string? 如何从两个 arrays 中创建一个对向量,然后使用 CUDA/Thrust 按对的第一个元素进行排序? - How do you make a pair vector out of two arrays and then sort by the first element of the pair using CUDA/Thrust? (C ++)访问向量对的第一列中第一个元素的第一个字符 - (C++) Accessing first character of first element in first column of vector pair 如何基于 .first 值从 std::pair 的 std::vector 中删除元素? - How to delete an element from a std::vector of std::pair based on .first value? 如何根据成对中的第一个和第二个元素对成对向量进行排序? - How do I sort a vector of pairs based on both first and second element in a pair? stable_sort 按对中的第一个元素在对的向量上按升序排列,没有比较器 C++ - stable_sort on a vector of pairs by first element in pair in increasing order without comparator function in C++ 如何在给定范围内对一对向量进行排序 - How to sort a pair of vector in some given range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM