合并两个地图c ++

时间:2017-10-19 12:18:11

标签: c++ maps

我创建了两张表格地图:

[事件编号] [质量]称为地图results_JMass

[事件编号] [动量]称为地图results_PhotonPt

两个映射都具有相同的键(事件编号),它是一个整数,并包含双精度值作为值。我想创建一个表格的第三张地图:

[事件编号] [质量] [动量]

我知道我需要使用

  std::map <Int_t, std::vector< pair <Double_t,Double_t> > > new_map; 

但我不确定如何将两张地图合并以创建第三张。

这就是我的想法:

   for (auto& kv3 : results_JMass) { //Iterates the data with kv3 as the iterator and the results_jMass what kv3 is iterating through
   event_number = kv3.first; //Iterator for the first index, i.e the events
   m = kv3.second;  //iterator for the second index i.e the masses

   new_map [event_number].first = m;

   for (auto&kv4: results_PhotonPt) {
 event_number = kv4.first;
 p = kv4.second;

 new_map [event_number].second = p;
   }
 }

3 个答案:

答案 0 :(得分:2)

假设你的意思是:

std::map < Int_t, pair <Double_t,Double_t> > new_map; 

迭代第一张地图,并使用以下内容将关键字和值添加到新地图中

new_map [event_number].first = mass;

然后遍历第二个地图,并将键和值添加到新地图:

new_map [event_number].second = momentum;

我假设2个源地图包含完全相同的事件编号列表,只是包含不同的数据 - 如果没有,您将在new_map中获得仅包含一个或其他数据的条目< / p>

答案 1 :(得分:1)

如果你100%确定,两个地图都有完全相同的键,那么一个循环就足够了:

std::map < Int_t, pair <Double_t,Double_t> > new_map;
auto it = second_map.begin();
for( const auto &p : first_map )
    new_map.emplace( p.first, std::make_pair( p.second, (it++)->second ) );

或C ++ 98

std::map < Int_t, pair <Double_t,Double_t> > new_map;
second_map_type::const_iterator it2 = second_map.begin();
for( first_map_type::const_iterator it1 = first_map.begin(); it1 != first_map.end(); ++it1 )
    new_map.insert( std::make_pair( it1->first, std::make_pair( it1->second, (it2++)->second ) ) );

答案 2 :(得分:0)

我们可以使用类似的算法来设置set_intersect,同时证明输入数据对于(几乎)零成本是正确的:

#include <map>
#include <utility>
#include <limits>

using mass_map = std::map<int, double>;
using moment_map = std::map<int, double>;

using mass_moment_map = std::map < int, std::pair <double,double> >;

bool merge_mass_and_moment(mass_map const& mass, 
                           moment_map const& moment,
                           mass_moment_map& target,
                           mass_moment_map& exceptions)
{
    target.clear();
    exceptions.clear();

    auto current_mass = mass.begin();
    auto current_moment = moment.begin();

    while (current_mass != mass.end() && current_moment != moment.end())
    {
        if (current_mass->first < current_moment->first)
        {
            exceptions.emplace(current_mass->first, 
                               std::make_pair(current_mass->second, std::numeric_limits<double>::infinity()));
            ++ current_mass;
        }
        else if (current_moment->first < current_mass->first)
        {
            exceptions.emplace(current_moment->first, 
                               std::make_pair(std::numeric_limits<double>::infinity(), current_moment->second));
            ++ current_moment;
        }
        else
        {
            target.emplace(current_moment->first, 
                           std::make_pair(current_mass->second, current_moment->second));
            ++current_mass;
            ++current_moment;
        }
    }

    while (current_mass != mass.end())
    {
        exceptions.emplace(current_mass->first, 
                            std::make_pair(current_mass->second, std::numeric_limits<double>::infinity()));
        ++ current_mass;
    }

    while(current_moment != moment.end())
    {
        exceptions.emplace(current_moment->first, 
                            std::make_pair(std::numeric_limits<double>::infinity(), current_moment->second));
        ++ current_moment;
    }

    return exceptions.empty();
}