【问题标题】:how to store pair key in map c++ [closed]如何在map c ++中存储对键[关闭]
【发布时间】:2022-01-02 02:07:50
【问题描述】:

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

3 1
2 1
3 2

3 1 的索引为 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;
}

【问题讨论】:

  • Images 不应用于文本数据,例如错误消息。 Code samples 应该最小、完整且具有代表性。
  • 您确定给定的输入会产生段错误吗?当我针对示例程序对其进行测试时,它没有。此外,屏幕截图中使用了不同的样本数据(其顶点数无效,超过了最大索引)。

标签: c++ dictionary data-structures graph breadth-first-search


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    让我们先讨论你的方法

    list<int>* adjLists;
    

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多