C++ 合并两个地图c++;

C++ 合并两个地图c++;,c++,maps,C++,Maps,我已经创建了两个表单地图: [事件编号][质量]称为地图结果 及 [活动编号][动量]称为地图结果 两个映射都具有相同的键(事件编号),该键是一个整数,包含两个值。我想创建表单的第三个映射: [活动编号][弥撒][动量] 我知道我需要使用 std::map <Int_t, std::vector< pair <Double_t,Double_t> > > new_map; 假设你的意思是: std::map < Int_t, pair <D

我已经创建了两个表单地图:

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

[活动编号][动量]称为地图结果

两个映射都具有相同的键(事件编号),该键是一个整数,包含两个值。我想创建表单的第三个映射:

[活动编号][弥撒][动量]

我知道我需要使用

  std::map <Int_t, std::vector< pair <Double_t,Double_t> > > new_map; 
假设你的意思是:

std::map < Int_t, pair <Double_t,Double_t> > new_map; 
然后迭代第二个映射,并将键和值添加到新映射:

new_map [event_number].second = momentum;
我假设两个源地图包含完全相同的事件编号列表,只是数据不同-如果没有,您将在
new\u map
中获得一些只包含一个或另一个数据的条目,假设您的意思是:

std::map < Int_t, pair <Double_t,Double_t> > new_map; 
然后迭代第二个映射,并将键和值添加到新映射:

new_map [event_number].second = momentum;

我假设两个源映射包含完全相同的事件编号列表,只是数据不同-如果没有,您将在
new\u 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 ) );
std::mapnew\u map;
auto it=second_map.begin();
用于(常数自动&p:第一张地图)
新的映射位置(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 ) ) );
std::mapnew\u map;
second_map_type::const_迭代器it2=second_map.begin();
for(first_map_type::const_iterator it1=first_map.begin();it1!=first_map.end();++it1)
新建映射。插入(std::make_pair(it1->first,std::make_pair(it1->second,(it2++->second));

如果您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 ) );
std::mapnew\u map;
auto it=second_map.begin();
用于(常数自动&p:第一张地图)
新的映射位置(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 ) ) );
std::mapnew\u map;
second_map_type::const_迭代器it2=second_map.begin();
for(first_map_type::const_iterator it1=first_map.begin();it1!=first_map.end();++it1)
新建映射。插入(std::make_pair(it1->first,std::make_pair(it1->second,(it2++->second));

我们可以使用类似的算法来设置_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();
}
#包括
#包括
#包括
使用mass_map=std::map;
使用矩_map=std::map;
使用质量矩映射=std::map;
布尔合并、质量和力矩(质量、映射常数和质量、,
力矩图常数和力矩,
质量矩图和目标图,
质量(力矩图和例外情况)
{
target.clear();
例外。清除();
自动当前质量=质量。开始();
自动当前力矩=力矩。开始();
while(当前质量!=mass.end()&当前力矩!=moment.end())
{
如果(当前质量->第一次<当前力矩->第一次)
{
例外情况。安放(当前质量->第一,
std::make_pair(当前质量->秒,std::numeric_limits::infinity());
++电流单位质量;
}
否则如果(当前力矩->第一次<当前质量->第一次)
{
例外情况。安放(当前时刻->第一,
std::make_pair(std::numeric_limits::infinity(),current_moment->second));
++当前力矩;
}
其他的
{
目标.安放(当前时刻->第一,
std::制造_对(当前_质量->秒,当前_力矩->秒);
++电流单位质量;
++当前力矩;
}
}
while(当前质量!=mass.end())
{
例外情况。安放(当前质量->第一,
std::make_pair(当前质量->秒,std::numeric_limits::infinity());
++电流单位质量;
}
while(当前力矩!=力矩.end())
{
例外情况。安放(当前时刻->第一,
std::make_pair(std::numeric_limits::infinity(),current_moment->second));
++当前力矩;
}
返回异常。empty();
}

我们可以使用类似的算法来设置_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();
}
#包括
#包括
#包括
使用mass_map=std::map;
使用矩_map=std::map;
使用质量矩映射=std::map;
布尔合并、质量和力矩(质量、映射常数和质量、,
力矩图常数和力矩,
质量矩图和目标图,
质量(力矩图和例外情况)
{
target.clear();
例外。清除();
自动当前质量=质量。开始();
自动当前力矩=力矩。开始();
while(当前质量!=mass.end()&当前力矩!=moment.end())
{
如果(当前质量->第一次<当前力矩->第一次)
{
例外情况。安放(当前质量->第一,
std::make_pair(当前质量->秒,std::numeric_limits::infinity());
++电流单位质量;
}
否则如果(当前力矩->第一次<当前质量->第一次)
{
例外情况。安放(当前时刻->第一,
std::make_pair(std::numeric_limits::infinity(),current_moment->second));
++当前力矩;
}
其他的
{
目标.安放(当前时刻->第一,
std::制造_对(当前_质量->秒,当前_力矩->秒);
++电流单位质量;
++当前力矩;
}
}
while(当前质量!=mass.end())
{
例外情况。安放(当前质量->第一,
std::make_pair(当前质量->秒,std::numeric_limits::infinity());
++电流单位质量;
}
while(当前力矩!=力矩.end())
{