我正在处理一个类似这样的数据结构:
map<string, set<string>> data;
到目前为止,我使用foreach循环来处理地图的工作没有问题,但现在我需要像这样打印地图中的数据:
KEY: elem1, elem2, elem3
KEY2: elem1, elem2, elem3
因为末尾缺少逗号,所以我不能再使用foreach循环了(对吗?)。由于我是C++、C++11和所有它可以提供的有趣内容的新手,我感到非常迷惑。
for ( auto i : data )
{
cout << i.first << ": ";
for ( size_t i = 0; i < /* size of the set */ - 1; i ++ )
cout << j << ", ";
cout << /* the last element of the set */ << endl;
}
我知道我想要什么,只是不懂语法,而且C++参考资料也没有帮助我多少。
谢谢你的回答,与此同时我会自己浏览C++参考资料。