如何初始化指针向量?

时间:2015-01-27 18:26:55

标签: c++ pointers vector

尝试运行函数initialize_graph时,我的程序崩溃了。我省略了所有其他功能。这是我第一次使用矢量。 graph.h标题将在main下面。我只需要有人帮我用头指针为空的图形对象初始化矢量。我可以从那里拿走它。谢谢你的期待。

#include <cstdlib>
#include <iostream>
#include <vector>
#include "graph.h"

using namespace std;

vector<graph*> adj_list; // one dimensional vector; each position in the vector stores a pointer to the head of a linked list


void graph::initialize_graph(int num_of_vertices, int num_of_edges)
{
    cout << "Doing push back"; // want to push back the same obj. call it graph* graph_obj (where head is null)
    //adj_list.resize(num_of_vertices); 
    graph *graph_obj;
    graph_obj -> head = NULL;
    for(int k = 0; k < num_of_vertices; k++)
    {
        adj_list.push_back(graph_obj); 
    }
    cout << "Pushback complete";
}

int main()
{
    int num_of_vertices, num_of_edges, vertex1, vertex2, function;
    graph graph_obj;


    while(1)
    {

     cout<<"1 - initialize graph" <<endl;
     cout<<"2 - insert an edge to the graph" <<endl;
     cout<<"3 - delete an edge from the graph" <<endl;
     cout<<"4 - list all edges in the graph" <<endl;
     cout<<"5 - list all of the neighbors for a particular vertex" << endl;
     cout<<"6 - list all of the vertices with no incoming edges" << endl << endl;

    cout<<"Choose a function (1 - 6): ";
    cin>>function;
    cout<<endl<<endl;

    switch(function)
    {
      case 1: 
             cout<<"Enter the number of vertices in the graph: ";
             cin>>num_of_vertices;
             cout<<endl<<"Enter the number of edges in the graph: ";
             cin>>num_of_edges;
             cout<<endl<<endl;
             cin.get();
             graph_obj.initialize_graph(num_of_vertices, num_of_edges);
             break;

     case 2: 
            cout<<"To enter an edge X -> Y (an edge from node X to node Y), use the following format: X Y (the names of the two vertices separated by a single space)" << endl;
            cout<<"Enter the edge to insert into the graph: ";
            cin>>vertex1>>vertex2;
           cout<<endl<<endl;
           graph_obj.insert_edge(vertex1, vertex2);         
           break;

     case 3: 
            cout<<"To enter an edge X -> Y (an edge from node X to node Y), use the following format: X Y (the names of the two vertices separated by a single space)" << endl;
            cout<<"Enter the edge to delete from the graph: ";
            cin>>vertex1>>vertex2;
            cout<<endl<<endl;
           graph_obj.delete_edge(vertex1, vertex2);         
           break;

     case 4:
             graph_obj.list_all_edges(num_of_vertices);
             break;

     case 5:   
            cout<<"Enter the vertex to list all of the neighbors for: ";
            cin>>vertex1;
            cout<<endl<<endl;
            graph_obj.list_all_neighbors(vertex1, num_of_vertices);
            break;

     case 6:
           graph_obj.no_incoming_edges(num_of_vertices);


    } //end switch



    }  //end while

    system("PAUSE");
    return 0;
}

class graph //class for the graph data structure
{
    public:  
     void initialize_graph(int num_of_vertices, int num_of_edges); //creates a new directed graph
     void insert_edge(int vertex1, int vertex2);  // inserts a directed edge (V1 - > V2) into the graph
     void delete_edge(int vertex1, int vertex2);    // deletes an edge (V1 -> V2) from the graph
     void list_all_edges(int num_of_vertices); // lists all of the edges in the graph
     void list_all_neighbors(int vertex1, int num_of_vertices); // lists all of the neighbors for a particular vertex
     void no_incoming_edges(int num_of_vertices);  // lists all of the vertices with no incoming edges

    private: 
    graph *prev; //pointer to the previous node in the linked list (used in the adjacency list implementation only)
    graph *next;  //pointer to the next node in the linked list    (used in the adjacency list implementation only)
    graph *head; //pointer to the head of a linked list            (used in the adjacency list implementation only)
    int edge;   // the id of the vertex that there is an edge to    (used in both implementations)

};

2 个答案:

答案 0 :(得分:3)

您实际上从未在堆中为指针分配新内存:

graph *graph_obj;

它的指针值未定义,这就是它崩溃的原因。

用以下内容初始化:

graph *graph_obj = new graph(); // Depending on your constructor.

编辑:此外,如果您要重新使用此指针并将其推回容器,您需要在容器中为每个元素创建一个新指针(不要忘记稍后删除它!)。

答案 1 :(得分:0)

这里有点问题:

graph *graph_obj;
graph_obj -> head = NULL;

您正在创建一个未初始化的指针,然后立即取消它。您需要为该指针创建一个指向的对象。