在控制台上打印字符串向量的最简单方法是什么?
我有类似的东西
map < int, vector<string>>
我希望将值打印到用户指定的密钥。
typemap::iterator p1;
cin >> i
for (pointer = map.begin(); pointer != map.end(); ++pointer)
{
if ((pointer->first) == i)
{
//The program should print the values right here.
}
}
答案 0 :(得分:2)
循环播放。并且不要遍历地图以找到密钥。
auto found = map.find(i);
if (found != map.end()) {
for (string const & s : found->second) {
cout << s << ' ';
}
}
答案 1 :(得分:2)
您可以使用std :: ostream_iterator
#include <algorithm>
#include <iterator>
/* .... */
auto found = map.find(i);
if (found != map.end()) {
std::copy(found->second.begin(),
found->second.end(),
std::ostream_iterator<std::string>(std::cout," "));
}
更多信息: http://www.cplusplus.com/reference/iterator/ostream_iterator/