将一对插入映射 vector 时出现 C++ 错误

标签 c++ visual-c++ visual-studio-2012

我正在尝试使用 map vector 创建图形。我实际上是在看一本书中的代码,并试图将其输入到 visual studios 2012 中,这样我就可以弄乱图表了。但出于某种原因,它不允许我将一对添加到 vector 中。下面的代码

创建 vector

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

将项目添加到 vector 中

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

最佳答案

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

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

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

检查 this出。

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

关于将一对插入映射 vector 时出现 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17848955/

相关文章:

c++ - 我可以获得编译器应用的优化日志吗?

c++ - 为什么 usernametwo 总是被跳过?

c++ - 用于删除 C++ 中的注释的程序不起作用

GetOpenFileName 时出现 C++ WinAPI 错误

c++ - 是否可以在 C++ 类中使用信号?

c++ - 编译器对模板的无效实例化给出错误

c++ - 如何使 Visual C++ 9 不发出实际上从未调用过的代码?

c++ - 从复杂函数返回变量以在另一个函数中使用

c++ - 按下 "Simple_window"按钮时,Stroustrup 的 "Next"会缩小

javascript - IIS:缺少图像、CSS 和脚本