Difference between vector <int> V[] and vector< vector<int> > V

25,607

Solution 1

vector<int> V[] is an array of vectors.

vector< vector<int> > V is a vector of vectors.

Using arrays are C-style coding, using vectors are C++-style coding.

Quoting cplusplus.com ,

Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

TL;DR:

When you want to work with a fixed number of std::vector elements, you can use vector <int> V[].

When you want to work with a dynamic array of std::vector, you can use vector< vector<int> > V.

Solution 2

One difference would be that although both can be initialized in the same way, e.g.

vector<int> V1[]        {{1,2,3}, {4,5,6}};
vector<vector<int>> V2  {{1,2,3}, {4,5,6}};

and accessed

cout << V1[0].back() << endl;
cout << V2[0].back() << endl;

the V1 can't grow. You cannot make V1.push_back(...) as its not a vector object. Its just an array. Second one is dynamic. You can grow it as you please.

Solution 3

vector<vector<int>> v(26); is a vector containing vectors. In this example, you have a vector with 26 vectors contained in it. The code v[1].push_back(x) means that x is pushed back to the first vector within the vectors.

vector<int> a[26]; is an array of vectors. In other words, a one-dimensional array containing 26 vectors of integers. The code a[1].push_back(x); has x being pushed back to the first index of the array.

Solution 4

vector<int> v[] is an array of vectors. That is, it is an array which contains vectors as its elements. So, you cannot change the size of the array part, but we can add to its elements which is vector.

For example,

1.vector<int> v1[] = {{1},{2},{3}};   // array that contains 3 vector elements.
2.vector<vector<int>> v2 = {{1},{2},{3}};  // vector that contains 3 vector elements.

So for the first we cannot change the size of v but we can add or delete elements to its elements since it is a vector.

v1.push_back(4);   // this will give an error since we cannot the predefined size of array.
v1[1].push_back(4); // this is acceptable since we are changing the vector part.

This makes the v1 {{1},{2,4},{3}}

For the second one, we can change both the overall size and its elements.

 v2.push_back(4);  // this is acceptable since it is vector.
Share:
25,607

Related videos on Youtube

Sakib Ahammed
Author by

Sakib Ahammed

want to know

Updated on October 05, 2021

Comments

  • Sakib Ahammed
    Sakib Ahammed over 2 years

    vector <int> V[] and vector< vector<int> > V both are 2D arrays.

    But what is the difference between them and where do we use each one? Please give a brief explanation.

    • Ben Voigt
      Ben Voigt over 9 years
      Neither of these is a 2-D array. Both are arrays of arrays. The only syntax available in C++ for a 2-D array is the one inherited from C. (Because std::array may have padding, see stackoverflow.com/q/19103244/103167)
    • DevSolar
      DevSolar over 9 years
      @BenVoigt: How can a gold-badge C++ grok call any of those two an "array of arrays"?
    • Ben Voigt
      Ben Voigt over 9 years
      @DevSolar: Because the English phrase "array of" is not especially precise. If I say "I have an array of Widgets", I haven't told you whether I'm managing that array with a smart pointer / wrapper collection, or whether I'm holding the Widgets directly, via composition, by reference (using a pointer of course, since you can't have a collection whose element type is a C++ reference type). So all of Widget[N], std::unique_ptr<Widget[]>, std::vector<Widget>, std::vector<WidgetBase*>, std::vector<std::unique_ptr<Widget>> are, in English, "array of Widget". To be specific, say the type
    • Ben Voigt
      Ben Voigt over 9 years
      In this case, there definitely are multiple arrays of int, (one held by each std::vector<int>), and these are themselves kept in another array.
    • DevSolar
      DevSolar over 9 years
      @BenVoigt: I would expect any coworker to not call a std::vector<> an "array", ever. That's all I am saying.
    • M.M
      M.M over 8 years
      @BenVoigt "2-D" C-style arrays are also actually arrays of arrays
  • tillaert
    tillaert over 9 years
    I would prefer std::array<vector<int>,n> with n the length of the array over std::array<vector>[] The latter is a mismash of C style and C++ style coding.
  • Ben Voigt
    Ben Voigt over 9 years
    @tillaert: Who gave you the idea that the built-in types are "not C++"?
  • tillaert
    tillaert over 9 years
    @BenVoigt I didn't say it's not C++, I was talking about coding styles.
  • juanchopanza
    juanchopanza over 9 years
    Also, you can't copy or assign the first one.