地图包含值作为列表+如何在C ++中打印(map contains value as a list+how to print in C++)

编程入门 行业动态 更新时间:2024-10-26 23:34:09
地图包含值作为列表+如何在C ++中打印(map contains value as a list+how to print in C++)

我有一个地图包含字符串作为键和文件名列表作为值。 例如: Map(firstDir, list(file1,file2,file3))

我知道通过使用下面的代码,我可以打印String的值

{ cout << "Key: " << pos->first << endl; cout << "Value:" << pos->second << endl; }

但是如果pos->second包含一个List,如何显示?

I have a map having strings as keys and lists of file names as values. ex: Map(firstDir, list(file1,file2,file3))

I know that by using following code I can print value which is of String

{ cout << "Key: " << pos->first << endl; cout << "Value:" << pos->second << endl; }

But if pos->second contains a List, how to display?

最满意答案

超载operator <<用于列表

std::ostream& operator << (std::ostream& out, std::list<ListElemType> const& lst) { for(std::list<ListElemType>::iterator it = lst.begin(); it != lst.end(); ++it) { if(it != lst.begin()) out << /*your delimiter*/; out << *it; } return out; }

现在你可以做你想做的事

cout << "Key: " << pos->first << endl << "Value:" << pos->second << endl;

overload operator << for list

std::ostream& operator << (std::ostream& out, std::list<ListElemType> const& lst) { for(std::list<ListElemType>::iterator it = lst.begin(); it != lst.end(); ++it) { if(it != lst.begin()) out << /*your delimiter*/; out << *it; } return out; }

now you can do what you want

cout << "Key: " << pos->first << endl << "Value:" << pos->second << endl;

更多推荐

本文发布于:2023-07-30 11:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地图   如何在   列表   print   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!