地图包含值作为列表+如何在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;更多推荐
发布评论