繁体   English   中英

如何在 map c++ 中存储对密钥

[英]how to store pair key in map c++

我试图做我的作业并且有这个 BFS 问题,我想将源和目标顶点以及它们的输入索引保存为 map 但事实是我不能。 我的输入不断收到此错误(您可以在终端中看到),我不知道该怎么做。 奇怪的是,当我输入2 1时没有错误,但输入3 1时它会崩溃。 这里的索引就像边的输入是:

3 1
2 1
3 2

3 1的索引为2 1的索引为 2。 [1]: https://i.stack.imgur.com/jZ3Aw.png

这是我的全部代码:

#include<iostream>
#include <list>
#include <stack>
#include <map>
using namespace std;
class Graph {
int numVertices;
list<int>* adjLists;
map<pair<int, int>, int> indices;
bool* visited;

public:
Graph(int vertices);
void addEdge(int src, int dest, int index);
void BFS(int startVertex,int size);
};

// Create a graph with given vertices,
// and maintain an adjacency list
Graph::Graph(int vertices) {
numVertices = vertices;
adjLists = new list<int>[vertices];
//    indices = new map<pair<int, int>, int >;
}

// Add edges to the graph
void Graph::addEdge(int src, int dest, int index) {
adjLists[src].push_back(dest);
adjLists[dest].push_back(src);
indices.insert(make_pair(make_pair(src, dest),index));
//cout<<index<<" "<<indices.[make_pair(src, dest)]<<endl;
}

// BFS algorithm
void Graph::BFS(int startVertex,int size) {
list<int> output;
int num=0;
visited = new bool[numVertices];
for (int i = 1; i <= numVertices; i++)
    visited[i] = false;

list<int> queue;

visited[startVertex] = true;
queue.push_back(startVertex);

list<int>::iterator i;

while (!queue.empty()) {
    int currVertex = queue.front();
    cout << "Visited " << currVertex << " ";
    queue.pop_front();

    for (i = adjLists[currVertex].begin(); i != adjLists[currVertex].end(); ++i) {
        int adjVertex = *i;
        if (!visited[adjVertex]) {
            visited[adjVertex] = true;
            num++;
            queue.push_back(adjVertex);
            cout<<indices[make_pair(currVertex, adjVertex)]<<endl;
            //output.push_back(indices[make_pair(currVertex, adjVertex)]);
        }
    }
}
cout<<endl<<num<<endl;
for (auto const& i : output) {
    std::cout << i<<" ";
}
}

int main() {
int m,n;
cin>>n>>m;
Graph g(n);
for (int i=1; i<=m; i++) {
    int u,v;
    cin>>u>>v;
    g.addEdge(u, v, i);
  }
g.BFS(1, n);
return 0;
}

您已经定义了 adjLists

list<int>* adjLists;

但是然后你写

adjLists[src].push_back(dest);

您不能直接访问列表的成员。 我很惊讶编译器通过了这个。

与这些其他序列容器相比,列表和 forward_lists 的主要缺点是它们无法通过 position 直接访问元素

https://www.cplusplus.com/reference/list/list/

我不能确定,但我认为您很有可能错误地访问adjLists 如果您只添加了三个顶点,那么您的构造函数将分配三个列表,位于adjLists[0]adjLists[1]adjLists[2] 请注意,索引从 0 开始,并以目标大小减 1 结束(索引表示从开始的偏移量,因此第一个元素是从开始的偏移 '0' 元素)。 当您尝试添加将顶点 3 链接到任何其他顶点的边时,它将尝试访问不存在的adjLists[3] 这就是导致错误访问的原因。 该错误有点误导,因为它似乎发生在std::list内部,但实际上是在 null std::list上调用push_back的结果。

让我们先讨论你的方法

list<int>* adjLists;

因此,访问 adjLists[] 非常好,它基本上只是访问带有索引的指针数组。 但是,如果它以 cpp 中的 0-index 开头。

因此,当您创建一个“n”索引数组时,您应该只访问该数组的 0...n-1 个值。 访问 adjLists[n] 将导致未定义的行为,并可能导致崩溃。

解决方案因此,要么在 Graph Constructor 中分配“n+1”,要么在添加边时将其值减 1 以从 0 开始,或者在您的输入中使用基于 0 的索引

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM