如何计算字符串向量?
How to cout a vector of strings?
在控制台上打印字符串向量的最简单方法是什么?
我得到了这样的东西
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.
}
}
有循环。并且不要遍历映射来查找键。
auto found = map.find(i);
if (found != map.end()) {
for (string const & s : found->second) {
cout << s << ' ';
}
}
您可以使用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/
在控制台上打印字符串向量的最简单方法是什么?
我得到了这样的东西
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.
}
}
有循环。并且不要遍历映射来查找键。
auto found = map.find(i);
if (found != map.end()) {
for (string const & s : found->second) {
cout << s << ' ';
}
}
您可以使用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/