简体   繁体   English

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

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

I trying to do my homework and there is this BFS question, I want to save the source and target vertices and the index of their input as a map but the thing is that I can't.我试图做我的作业并且有这个 BFS 问题,我想将源和目标顶点以及它们的输入索引保存为 map 但事实是我不能。 I keep getting this error for my input(which you can see down in terminal) and I don't know what to do.我的输入不断收到此错误(您可以在终端中看到),我不知道该怎么做。 the strange thing is that when I input 2 1 it's fine without error but for 3 1 it crashes.奇怪的是,当我输入2 1时没有错误,但输入3 1时它会崩溃。 index in here is like if the input of edges are:这里的索引就像边的输入是:

3 1
2 1
3 2

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

this is my whole code:这是我的全部代码:

#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;
}

You have defined adjLists您已经定义了 adjLists

list<int>* adjLists;

but then you write但是然后你写

adjLists[src].push_back(dest);

You cannot directly access a member of a list.您不能直接访问列表的成员。 I am surprised the compiler passes this.我很惊讶编译器通过了这个。

The main drawback of lists and forward_lists compared to these other sequence containers is that they lack direct access to the elements by their position与这些其他序列容器相比,列表和 forward_lists 的主要缺点是它们无法通过 position 直接访问元素

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

I can't be certain but I think there's a good chance that you're incorrectly accessing adjLists .我不能确定,但我认为您很有可能错误地访问adjLists If you've only added three vertices, then your constructor will allocate three lists, located at adjLists[0] , adjLists[1] and adjLists[2] .如果您只添加了三个顶点,那么您的构造函数将分配三个列表,位于adjLists[0]adjLists[1]adjLists[2] Note that indexing starts at 0, and ends at your target size minus 1 (the index represents the offset from the start, so the first element is offset '0' elements from the start).请注意,索引从 0 开始,并以目标大小减 1 结束(索引表示从开始的偏移量,因此第一个元素是从开始的偏移 '0' 元素)。 When you attempt to add an edge linking vertex 3 to any other vertex, it will attempt to access adjLists[3] , which doesn't exist.当您尝试添加将顶点 3 链接到任何其他顶点的边时,它将尝试访问不存在的adjLists[3] This is what causes the bad access.这就是导致错误访问的原因。 The error is a bit misleading because it seems to happen inside std::list but is actually a result of calling push_back on a null std::list .该错误有点误导,因为它似乎发生在std::list内部,但实际上是在 null std::list上调用push_back的结果。

Lets discuss your approach first让我们先讨论你的方法

list<int>* adjLists;

So, its perfectly fine to access adjLists[] which is basically just accessing an array of pointers with an index.因此,访问 adjLists[] 非常好,它基本上只是访问带有索引的指针数组。 However, if it starts with 0-index in cpp.但是,如果它以 cpp 中的 0-index 开头。

So when you create a "n" index array you should only access 0...n-1 values of the array.因此,当您创建一个“n”索引数组时,您应该只访问该数组的 0...n-1 个值。 Accessing adjLists[n] will result in undefined behaviour and can can result in a crash.访问 adjLists[n] 将导致未定义的行为,并可能导致崩溃。

Solution So either allocate "n+1" in the Graph Constructor or when adding an edge decrement its values by 1 to start from 0 or in your input use 0 based indices解决方案因此,要么在 Graph Constructor 中分配“n+1”,要么在添加边时将其值减 1 以从 0 开始,或者在您的输入中使用基于 0 的索引

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

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