在multimap中打印多图(嵌套地图)

时间:2014-06-24 04:37:09

标签: c++ map nested multimap

我正在尝试打印出嵌套的多图,但是没有找到方法(或在此讨论)来帮助我解决问题。

他们通常打印多图表看起来像这样:

template<typename a, typename b>
void printMMap1(std::multimap<a, b>thing){
 for (std::multimap<a,b>::iterator it = thing.begin(); it != thing.end(); it++){
    std::cout << it->first << " : " << it->second << std::endl;
  }
}

但是现在我想用同样的动机打印一个:

multimap<multimap<a,b>, multimap<c,d>> MAPname;

这似乎不起作用:

template<typename aa, typename bb, typename cc>
void printMMap(std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>thing){
    for (std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>::iterator it = thing.begin(); it != thing.end(); it++){
    std::cout << it->first.first << " : " << it->first.second << std::endl <<
            it->second.first << " : " << it->second.second << std::endl;
}

}

任何帮助/建议将不胜感激!

感谢您的帮助。

_EDIT:

使用来自hansmaad的例子的动机,我提出了一些接近我想要的解决方案(下面):

//N.B: I removed the "auto"s for educational purposes (mostly for myself and other beginners)

template<typename a, typename b>
void print1(const std::multimap<a, b>& thing){
for (std::multimap<a,b>::const_iterator it = begin(thing); it != thing.end(); it++){
   std::cout << it->first << " : " << it->second << std::endl;
   }
}



template<typename aa, typename bb, typename cc>
void print2(const std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>& thing){
std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>::const_reverse_iterator it = thing.rbegin();

//why reverse iterator? Because I noticed that the loop which duplicates the output has a final iteration equal to the desired output, so I only use the last iteration i.e. going backwards ("rbegin")

std::multimap<aa, bb> keyMap = it->first;
std::multimap<aa, cc> valueMap = it->second;

std::cout << "key\n";
print1(keyMap);
std::cout << "value\n";
print1(valueMap);

}

这打印出这个解决方案,它接近我想要的90%。对于例如它打印:

key
a_key1 : a_value1
a_key2 : a_value1
a_key2 : a_value2
a_key2 : a_value3
a_key3 : a_value1
a_key3 : a_value2
a_key3 : a_value3
a_key3 : a_value4

value
b_key1 : b_value1
b_key1 : b_value2
b_key1 : b_value3
b_key1 : b_value4
b_key2 : b_value1
b_key3 : b_value1
b_key4 : b_value1
b_key4 : b_value2

我希望打印相同的输出,尽管格式如下:

key                  value 
a_key1 : a_value1    b_key1 : b_value1
a_key2 : a_value1    b_key1 : b_value2
a_key2 : a_value2    b_key1 : b_value3
a_key2 : a_value3    b_key1 : b_value4
a_key3 : a_value1    b_key2 : b_value1
a_key3 : a_value2    b_key3 : b_value1
a_key3 : a_value3    b_key4 : b_value1
a_key3 : a_value4    b_key4 : b_value2

略有不同。我闻到我很亲密。

1 个答案:

答案 0 :(得分:0)

我不确定您对printMMap函数的输出结果是什么,但您的代码问题是it->first.first

template<typename aa, typename bb, typename cc>
void printMMap(std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>thing){
    for (std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>::iterator it = thing.begin(); it != thing.end(); it++){
            // std::multimap<aa, bb> x = it->first;
            // std::multimap<aa, bb> y = it->second;
            // x, y don't have a member 'first', they are multimaps
            // broken:
            std::cout << it->first.first /*...*/ it->second.second << std::endl;
    }
}

你可以这样做:

template<typename a, typename b>
void print(const std::multimap<a, b>& thing){
    for (auto it = begin(thing); it != end(thing); ++it){
        std::cout << it->first << " : " << it->second << std::endl;
    }
}

template<typename aa, typename bb, typename cc>
void print(const std::multimap<std::multimap<aa, bb>, std::multimap<aa, cc>>& thing){
    for (auto it = begin(thing); it != end(thing); ++it){
        auto& keyMap = it->first;
        auto& valueMap = it->second;
        std::cout << "key\n";
        print(keyMap);
        std::cout << "value\n";
        print(valueMap);
    }
}

将打印

    multimap<multimap<int, int>, multimap<int, int>> mmap;
    multimap<int, int> key{ { 1, 1 }, { 1, 2 }, { 2, 4 } };
    multimap<int, int> value{ { 10, 1 } };
    mmap.emplace(move(key), move(value));
    print(mmap);


>key
>1 : 1
>1 : 2
>2 : 4
>value
>10 : 1