将一对插入到映射的向量中时出现视觉C++错误
visual C++ error when inserting a pair into vector of maps
我正在尝试使用映射向量创建一个图。事实上,我正在看一本书中的代码,并试图将其输入到2012年的visual Studio中,这样我就可以处理图形了。但由于某种原因,它不允许我在向量中添加一对。低于的代码
创建矢量
//vector that holds a map of all adjacent vertices
vector<map<int, int> > adjList;
图类的构造函数
Graph::Graph(int n){
map<int, int> element;
adjList.assign(n, element);
}
将项目添加到矢量
int v1 = e.v1;
int v2 = e.v2;
int weight = e.weight;
//add the first vertix the edge connects to intto the adjList
adjList.insert(make_pair(v1, weight));
//add the second vertix the edge connects to into the adjList
adjList.insert(make_pair(v2, weight));
错误我得到的视觉工作室2012当试图编译
Error 1 error C2661: 'std::vector<_Ty>::insert' : no overloaded function takes 1 arguments c:userselliotdocumentsvisual studio 2012projectsgraphgraph.cpp 25 1 Project1
Error 2 error C2661: 'std::vector<_Ty>::insert' : no overloaded function takes 1 arguments c:userselliotdocumentsvisual studio 2012projectsgraphgraph.cpp 27 1 Project1
我想我可以在评论中说清楚,但让我们更详细一些。你有一个地图矢量。您正试图将一对某些值插入到一个映射向量中。当然,这是不可能的(这不是python)。你应该也可以这样做:
adjList[0].insert(make_pair(v1, weight));
或者任何其他需要插入的索引。
看看这个。
我猜如下。每个节点都是一个数字(它的id是一个整数)。所以,用这个数字,你索引一个向量,得到它的邻接列表。相邻列表是一张地图。地图中的每个条目都是另一个邻居的id,可能还有边缘的长度。例如,如果你想要ID为3的节点的邻居,你会要求adjList[2](它们可能是从0开始索引的),并获得它的邻居的映射。
insert
成员函数采用两个参数:一个位置和一个值。如果您不关心指定位置,那么只需使用push_back
函数。您可能还有其他问题,例如类型问题,但这是您的直接问题。
你不应该认为你的编译器在胡言乱语。它告诉你到底出了什么问题:
没有重载函数需要1个参数
快速浏览一下方便的参考资料,看看它是正确的:
iterator insert (const_iterator position, const value_type& val);
iterator insert (const_iterator position, size_type n, const value_type& val);
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);
iterator insert (const_iterator position, value_type&& val);
iterator insert (const_iterator position, initializer_list<value_type> il);
相关文章:
- 为什么我在尝试模板时遇到视觉工作室C++错误
- C++11 中好友模板类的视觉C++错误
- 错误 C2512 视觉C++(并且不是循环依赖项)
- 是视觉C++优化器错误还是我的代码中有错误?
- 视觉错误:变量或字段'Palindrome'在 C++ 中声明为 void
- 视觉C++错误 C2451 条件字符串是非法的
- 视觉C++错误"无法将参数 1 从'句柄'转换为'句柄 &'"
- 视觉C 错误LNK2019
- Qt QQuickView视觉错误/调整大小时闪烁
- 视觉C++错误:函数必须返回一个值
- 视觉错误类 c++ 类。 不知道它是什么
- 视觉C++错误 C2065 未声明的标识符
- 视觉错误LNK2001:未解析的外部符号C++
- 与 C++ 编译文件相关的视觉错误
- 视觉C++错误 C2143:语法错误:'constant'之前缺少';'
- 将一对插入到映射的向量中时出现视觉C++错误
- 视觉错误LNK2001:未解析的外部符号(C++)
- 视觉C++错误 C2143:语法错误:'{'之前缺少';'
- 视觉错误在使用 #include "DarkGDK.h" 编译程序时出现C++
- 视觉C++错误 C3867 和 C2664