如何迭代多个地图并获取其值? -C ++

问题描述 投票:1回答:1

因此,我有3个都具有相同键的多图(在这种情况下,这是日期),并且它们代表的值对于每个图都是不同的。我知道如何创建迭代器并在一个地图上进行迭代,然后使用该迭代器获取该地图的值。但是,我将如何遍历所有3个映射并将它们的值放入所需的变量中?我很困惑,因为如果我使用一个迭代器来遍历所有3个映射,那么该迭代器只会为我提供为其创建迭代器的映射的值,而且我觉得为此创建3个for循环有点多。有什么想法吗?

    multimap<Date, float> mapOption4WindSpeed;
    multimap<Date, float> mapOption4Temperature;
    multimap<Date, double> mapOption4Solar;


    cout << "Please enter year (xxxx): " << endl;
    cin >> year;

    Date newDate;
    newDate.SetDay("1");
    newDate.SetDay("1");
    newDate.SetDay(year);

    for(int i = 0; i < SIZE; i++)
    {
        mapOption4WindSpeed.insert(make_pair(windlog[i].d, windlog[i].speed.GetSpeed()));
        mapOption4WindSpeed.insert(make_pair(windlog[i].d, windlog[i].temp.GetTemperature()));
        mapOption4Solar.insert(make_pair(windlog[i].d, windlog[i].solar.GetSolar()));
    }

    multimap<Date, float>::iterator itS = mapOption4WindSpeed.find(newDate);
    multimap<Date, float>::iterator itT = mapOption4Temperature.find(newDate);
    multimap<Date, double>::iterator itSol = mapOption4Solar.find(newDate);

    for(int i = 0; i < SIZE; i++) //Loop through entire array to check if the input matches the year
    {

        if(itS != mapOption4WindSpeed.end()) //This statement and the above is where I am confused
        {

            //Bunch of operations in here

        }

我的结构:

typedef struct
{

    Date d;
    Time t;
    Weather speed;
    Weather solar;
    Weather temp;

} WindLogType;

然后我使用结构的Vector来访问值:

Vector<WindLogType> windlog;
c++ maps
1个回答
0
投票

所以我有3个都具有相同键的多图(在这种情况下为日期),并且它们代表的值对于每个图都是不同的。

除非您有非常强烈的理由要使用三个multimap,我建议创建一个multimap

struct MyData
{
   float windSpeed;
   float temperature;
   double solar;
};

std::multimap<Date,  MyData> mapOfMyData;

这将使访问与Date相对应的所有数据变得容易。

© www.soinside.com 2019 - 2024. All rights reserved.