首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在multimap中打印multimap (嵌套映射)

在multimap中打印multimap (嵌套映射)
EN

Stack Overflow用户
提问于 2014-06-24 04:37:09
回答 1查看 820关注 0票数 0

我正在尝试打印一个嵌套的multimap,但是还没有找到一种方法(或者这里的讨论)来帮助我解决这个问题。

我通常用这种方式打印一个multimap,看起来如下所示:

代码语言:javascript
复制
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;
  }
}

但现在我想用同样的动机打印a:

代码语言:javascript
复制
multimap<multimap<a,b>, multimap<c,d>> MAPname;

这似乎行不通:

代码语言:javascript
复制
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的例子中的动机,我想出了一个接近我想要的解决方案的方法(如下所示):

代码语言:javascript
复制
//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%。例如,印刷品:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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

有点不同。我闻到我快到了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-24 06:26:54

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

代码语言:javascript
复制
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;
    }
}

例如,您可以这样做:

代码语言:javascript
复制
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);
    }
}

将打印出来

代码语言:javascript
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24378411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档