我正在尝试打印一个嵌套的multimap,但是还没有找到一种方法(或者这里的讨论)来帮助我解决这个问题。
我通常用这种方式打印一个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;
}
}但现在我想用同样的动机打印a:
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有点不同。我闻到我快到了。
发布于 2014-06-24 06:26:54
我不确定您期望从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 : 1https://stackoverflow.com/questions/24378411
复制相似问题