简体   繁体   中英

vector initialization in c++

Can someone explain to me why verticies.size() in Graph constructor equals '4' but when called again in addEdge verticies.size() equals 0? The program crashes at verticies[fromVertex].addEdge(toVertex) because the size of verticies is 0.

I'm sure this is something I should know, but I don't where I am going wrong.

class Vertex {

public:
    int value;
    vector<int> adj;
    bool isVisited = false;

    Vertex(int _value)
    {
        value = _value;
    }

    void addEdge(int destination)
    {
        adj.push_back(destination);
    }   
};

class Graph
{

public:

    int vertexCount;    // No. of vertices            
    vector<Vertex> verticies;

    Graph(int _vertexCount)
    {
        this->vertexCount = _vertexCount;
        vector<Vertex> verticies;
        for (size_t i = 0; i < _vertexCount; i++)
        {
            Vertex v = Vertex(i);
            verticies.push_back(v);
        }
        cout << "verticies count " << verticies.size() << endl;
    }
    void addEdge(int fromVertex, int toVertex)  
    {
        cout << "verticies count in addEdge: " << verticies.size() << endl;
        verticies[fromVertex].addEdge(toVertex); 
    }
    void findPath(int fromVertex, int toVertex, vector<int> pathSoFar)  
    {
        pathSoFar.push_back(fromVertex);
        if (fromVertex == toVertex)
        {
            for (int i = 0; i < pathSoFar.size(); i++)
            {
               cout << pathSoFar.at(i) << " ";
            }
            cout << endl;
            return;
        }
        else
        {
            for (size_t i = 0; i < verticies[fromVertex].adj.size(); i++)
            {   
                int nextToVisit = verticies[fromVertex].adj.at(i);
                if (verticies[nextToVisit].isVisited == false)
                {
                    findPath(nextToVisit, toVertex, pathSoFar);
                }

            }
        }
    }
};

int main()
{
    // Create a graph given in the above diagram
    Graph g(4);
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(3, 3);


    vector<int> path;
    g.findPath(2, 1, path);

    return 0;
}

Can someone explain to me why verticies.size() in Graph constructor equals '4' but when called again in addEdge verticies.size() equals 0?

This is because those are two different objects. Here

Graph(int _vertexCount)
{
    this->vertexCount = _vertexCount;
    vector<Vertex> verticies;
    // ....
}

You are creating a local vector called verticies that shadows the member variable. Simply removing that line should be fine. I mean you already declared the member, no need to do it twice ;).

Moreoever, I would suggest you to either resize the vector before filling it with data, or at least reserve to the capacity you need.

Here you are shadowing your class field:

Graph(int _vertexCount)
{
    this->vertexCount = _vertexCount;
    vector<Vertex> verticies;  // <--- exactly here
    for (size_t i = 0; i < _vertexCount; i++)
    {
        Vertex v = Vertex(i);
        verticies.push_back(v);
    }
    cout << "verticies count " << verticies.size() << endl;
}

And you are doing all the operations on the local (in the context of the constructor) verticies instead of Graph::verticies . BTW, correct English plural form is vertices .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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