我正在尝试创建图的邻接表示。

我使用向量矢量编写了一个小程序,但是我不断收到“分段错误”,但是编译器(Windows上的clang ++版本5.0.1)似乎总是尝试访问向量vertex_matrix给出分段错误,为什么是它没有被实例化?

这是标题:

#ifndef GRAPH_MATRIX
#define GRAPH_MATRIX
#include <vector>
//header for graph represented via adjacency matrix with minimal functionality
class graph
{
    public:
    graph(int);
    ~graph();
    void add_edge(int v1, int v2, int weight);
    void print_graph();

    private:
    std::vector<std::vector<int>> vertex_matrix;
    int num_of_vertices;
    int num_of_edges;


};
#endif


这是cpp实现:

#include <iostream>
#include "graph_matrix.h"
#include <climits>
using namespace std;
//header for graph represented via adjacency matrix with minimal functionality

graph::graph(int _num_of_vertices) : num_of_vertices(_num_of_vertices)
    {

        if (_num_of_vertices==0)
        {
            _num_of_vertices=10;
        }

        for (int i = 0; i < _num_of_vertices; i++)
        {
             vertex_matrix[i]=(vector<int> (_num_of_vertices,INT_MAX));
        }


    }

    graph::~graph()
    {
    vertex_matrix.clear();
    }

    void graph::add_edge(int v1, int v2, int weight)
    {
        //vertex_matrix[v1-1][v2-1] == INT_MAX

        vector<int> columnVector = vertex_matrix[v1-1];
        if (columnVector[v2-1] == INT_MAX)
            {
                columnVector[v2-1] = weight;
            }
    }

void graph::print_graph()
{
    cout << "vertex_matrix size:" << vertex_matrix.size() << endl;
    for (int i=0; i< num_of_vertices; i++)
    {

        for (int j = 0; j < num_of_vertices; j++)
            {
                //vertex_matrix[i][j]
                std::vector<int> columnVector = vertex_matrix[i];

                if (columnVector[j] != INT_MAX)
                {
                    std::cout << columnVector[j] ;
                }
                else
                {
                        std::cout << "0";
                }

            }

            std::cout << endl;
    }//end for printing


}


这是主要条目:

#include <iostream>
#include "graph_matrix.h"
using namespace std;
int main ()
{
std::cout << " Matrix representation of graph" << std::endl;

    graph _graph(4);
    _graph.add_edge(1,2,1);
    _graph.add_edge(2,3,1);
    _graph.add_edge(3,1,1);
    _graph.add_edge(3,3,1);
    _graph.add_edge(3,4,1);
    _graph.add_edge(4,0,0);

    _graph.print_graph();


}


我编辑了上面的代码以使用按引用传递,但是矩阵仍然显示为0。

请通过参考传递帮助,以下更新:

标头:

#ifndef GRAPH_MATRIX
#define GRAPH_MATRIX
#include <vector>
//header for graph represented via adjacency matrix with minimal functionality
class graph
{
    public:
    graph(int);
    ~graph();
    void add_edge(int v1, int v2, int weight,std::vector<std::vector<int>> & matrix);
    void print_graph();
    std::vector<std::vector<int>> vertex_matrix;


    private:
    int num_of_vertices;
    int num_of_edges;


};
#endif


Cpp文件:

#include <iostream>
#include "graph_matrix.h"
#include <climits>
using namespace std;
//header for graph represented via adjacency matrix with minimal functionality

graph::graph(int _num_of_vertices) : num_of_vertices(_num_of_vertices) {

    if (num_of_vertices == 0) {
        num_of_vertices = 10;
    }

    for (int i = 0; i < num_of_vertices; i++) {

         std::vector<std::vector<int>>& matrix = vertex_matrix;
         matrix.push_back(vector<int> (num_of_vertices, INT_MAX));
    }

}

graph::~graph() {
    std::vector<std::vector<int>>& matrix = vertex_matrix;
    matrix.clear();
}

void graph::add_edge(int v1, int v2, int weight,std::vector<std::vector<int>> & _matrix) {

    //vertex_matrix[v1-1][v2-1] == INT_MAX
    vector<int> columnVector = _matrix[v1 - 1];
    if (columnVector[v2 - 1] == INT_MAX) {
        columnVector[v2 - 1] = weight;

    }


}


void graph::print_graph() {

     std::vector<std::vector<int>>& matrix = vertex_matrix;

    for (int i = 0; i < matrix.size(); i++) {

        for (int j = 0; j < matrix.size(); j++) {
            //vertex_matrix[i][j]
            std::vector<int> columnVector = matrix[i];

            if (columnVector[j] != INT_MAX) {
                std::cout << columnVector[j];
            } else {
                std::cout << "0";
            }


        }

        std::cout << endl;
    }//end for printing


}


主要:

#include <iostream>
#include "graph_matrix.h"
using namespace std;
int main ()
{
std::cout << " Matrix representation of graph" << std::endl;

    graph _graph(4);
             std::vector<std::vector<int>>& m =  _graph.vertex_matrix;
    _graph.add_edge(1,2,1,m);
    _graph.add_edge(2,3,1,m);
    _graph.add_edge(3,1,1,m);
    _graph.add_edge(3,3,1,m);
    _graph.add_edge(3,4,1,m);
    _graph.add_edge(4,0,0,m);

    _graph.print_graph();



}


任何帮助将不胜感激。

谢谢

最佳答案

您创建一个空的向量,然后尝试访问其中的元素。将您的构造函数更改为

graph::graph(size_t _num_of_vertices) :
    vertex_matrix(
        std::vector<std::vector<int>>(
            _num_of_vertices,std::vector<int>(_num_of_vertices)
        )
    )
{}


创建正确大小的向量。

同样,在_num_vertices == 0的情况下,将其设置为10,但是那是在初始化成员num_vertices之后,因此将对象置于不一致状态。有多种解决方法,当传递的顶点数为零时,我可能会抛出异常,或者将其忽略。用户想要零尺寸的矩阵吗?为什么不?

此外,大小应为unsigned不带符号,容器大小为size_t。甚至更好的是,您根本不应该拥有该成员,因为vector已经知道其大小,重复该信息的唯一原因是引入错误;)

10-08 05:41
查看更多