【问题标题】:Range of the first element of a vector pair向量对的第一个元素的范围
【发布时间】:2018-06-13 15:49:35
【问题描述】:

我需要找到向量对的第一个元素的范围。我需要这个范围作为地图,它计算这个向量中的重复条目。 这是一个代码片段以及我如何管理它。也许还有其他更好的解决方案?

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 )

【问题讨论】:

    标签: c++


    【解决方案1】:

    是的,第二步完全是多余的。您只需遍历容器,每当您需要该对的第一个元素时,您就可以像在第 2 步中那样明确地说出来。

    for(const pair<unsigned int,Point>& element : Roi_Num_Koord)
    {
        ++frequency[element.first];
        cout << "freque " << frequency[element.first] << endl;        
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-03
      • 2021-10-23
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多