本意是循环打印key和value,但是下一个key的value跟随在了上一个key的value后面,有什么解决办法吗
#include <iostream>
#include <string>
#include <vector>
#include <map>;
using namespace std;
//运算符重载,使 << 可以直接输出map.second
ostream& operator<<(ostream& out, vector<string> v1)
{
for (auto& it : v1)
{
out << it <<' ';
}
return out;
}
void test01()
{
vector<string> vs1;
vs1.push_back("111");
vs1.push_back("222");
vs1.push_back("333");
vs1.push_back("444");
vector<string> vs2;
vs1.push_back("555");
vs1.push_back("666");
vs1.push_back("777");
vs1.push_back("888");
map<int, vector<string>> newMap;
newMap.insert(pair<int, vector<string>>(2, vs1));//向map容器插入数据
newMap.insert(pair<int, vector<string>>(3, vs2));//向map容器插入数据
for (auto& pos22 : newMap)
{
cout << pos22.first << ' ' << pos22.second << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
pos22
应该是 vector<string>
吧,vector
数组没有 first
和second
成员。
已经定义了重载,直接:
cout << pos22;