在 STL 映射中使用向量作为参数
Using vector as an argument in STL map
我一直在尝试 link 2 个值和 1 个键,我发现的一种方法是使用向量来做同样的事情。我写了下面的代码
#include<iostream>
#include<vector>
#include<map>
#include<stdlib.h>
using namespace std;
map<int,vector<int> map1;
void insertInMap(int q,int a,int b)
{
vector<int> v1;
v1.push_back(a);
v1.push_back(b);
map1.insert(q,v1);
}
int main()
{
return 0;
}
insertinmap函数是创建一个向量作为地图的参数。我在初始化列表时遇到错误
错误 - 模板 2 参数无效,模板 4 参数无效。
在地图中,insert()
期望将一个元素作为参数插入。映射的元素是由键和值组成的一对。所以:
map1.insert(make_pair(q,v1));
在地图中插入元素的更方便的方法是结合使用赋值运算符和索引:
map1[q] = v1;
注意:您在地图的定义中忘记了结束符 >
,但我猜这是一个错字
我一直在尝试 link 2 个值和 1 个键,我发现的一种方法是使用向量来做同样的事情。我写了下面的代码
#include<iostream>
#include<vector>
#include<map>
#include<stdlib.h>
using namespace std;
map<int,vector<int> map1;
void insertInMap(int q,int a,int b)
{
vector<int> v1;
v1.push_back(a);
v1.push_back(b);
map1.insert(q,v1);
}
int main()
{
return 0;
}
insertinmap函数是创建一个向量作为地图的参数。我在初始化列表时遇到错误
错误 - 模板 2 参数无效,模板 4 参数无效。
在地图中,insert()
期望将一个元素作为参数插入。映射的元素是由键和值组成的一对。所以:
map1.insert(make_pair(q,v1));
在地图中插入元素的更方便的方法是结合使用赋值运算符和索引:
map1[q] = v1;
注意:您在地图的定义中忘记了结束符 >
,但我猜这是一个错字