c++arraysvector

Vector of Arrays or Array of vectors cpp


int n = 10;  
vector<int> adj[n];

Does this line create an array of vectors or a Vector of Arrays. And how is it different from

vector<vector <int>> vect;

Solution

  • vector<int> adj[n];
    

    Creates an array of n vectors of ints. However, if n is not a compile time constant, this is not standard C++, which does not support variable length arrays. Some compilers may implement it as an extension.

    vector<vector<int>> vect;
    

    This creates a vector of vectors of ints.

    The dimensions of the latter are no longer fixed, which makes them functionally quite different. vect can contain any number of vector<int> values.

    There are also significant ramifications to using std::vector or std::array vs. raw arrays when it comes to passing results to/from functions.