map包含值作为列表+如何在C++中打印

sap*_*sap 4 c++

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

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

{
    cout << "Key: " << pos->first << endl;
    cout << "Value:" << pos->second << endl;
}
Run Code Online (Sandbox Code Playgroud)

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

Arm*_*yan 7

超载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;
}
Run Code Online (Sandbox Code Playgroud)

现在你可以做你想做的事

cout << "Key: " << pos->first << endl << "Value:" << pos->second << endl; 
Run Code Online (Sandbox Code Playgroud)