簡體   English   中英

合並兩個地圖C ++

[英]Merging two maps c++

我已經創建了兩種形式的地圖:

[活動編號] [大量]稱為地圖結果_JMass

[活動編號] [動量]稱為地圖結果_PhotonPt

兩個映射都具有相同的鍵(事件號),該鍵是整數,並且包含Doubles作為值。 我想創建表單的第三張地圖:

[活動編號] [質量] [動量]

我知道我需要用

  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;
   }
 }

假設您的意思是:

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中獲得一些僅包含一個或另一個數據的條目

如果您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 ) ) );

我們可以使用類似的算法來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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM