1

我正在尝试使用地图矢量创建图形。我实际上是在查看书中的代码并尝试将其输入到 Visual Studio 2012 中,这样我就可以弄乱图表了。但由于某种原因,它不允许我在向量中添加一对。下面的代码

创建向量

//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));

尝试编译时我从 Visual Studios 2012 得到的错误

Error   1   error C2661: 'std::vector<_Ty>::insert' : no overloaded function takes 1 arguments  c:\users\elliot\documents\visual studio 2012\projects\graph\graph.cpp   25  1   Project1
Error   2   error C2661: 'std::vector<_Ty>::insert' : no overloaded function takes 1 arguments  c:\users\elliot\documents\visual studio 2012\projects\graph\graph.cpp   27  1   Project1
4

2 回答 2

2

我想我可以在评论中说清楚,但让我们更详细一点。你有一个地图矢量。您正在尝试将一对某些值插入到地图向量中。那当然是不可能的(这不是 python)。你应该和可以做的是这样的:

adjList[0].insert(make_pair(v1, weight));

或您需要在其中插入内容的任何其他索引。

看看这个

我的猜测如下。您的每个节点都是一个数字(它的 id 是一个整数)。因此,使用该数字,您可以索引一个向量并获取它的邻接列表。邻接表是一张地图。地图中的每个条目都是另一个邻居的 id,可能是边的长度。因此,例如,如果您想要 ID 为 3 的节点的邻居,您将要求 adjList[2] (它们可能从 0 开始索引)并获取其邻居的地图。

于 2013-07-25T04:32:48.500 回答
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);
于 2013-07-25T04:24:19.313 回答