C++

Vector Declaration Differences/ vector<int> adj(), vector<int> adj[], vector<vector<int>> adj

9taetae9 2023. 9. 14. 06:53
728x90

difference between vector<int> adj(4); and vector<int> adj[4]; ?

 

The two statements you provided are C++ code related to the Standard Template Library's (STL) vector container. They declare vector objects in different ways. Here's the distinction between them

1)

vector<int> adj(4);After this declaration, adj can be treated as a vector with 4 elements, all of which are initialized to 0.
This creates a single vector object named adj. The vector contains four integers (initialized to zero by default). 

The (4) indicates the initial size of the vector.                                                                                                                          adj[0] = 5;
adj[1] = 10;
adj[2] = 15;
adj[3] = 20;


2)

vector<int> adj[4];
This creates an array of 4 vector<int> objects. Each element of the array is itself a vector<int>.
After this declaration, you can treat adj as an array where each element is a vector.
Example usage:

adj[0] = 5;
adj[1] = 10;
adj[2] = 15;
adj[3] = 20;

 

Let's visualize it.

1)

vector<int> adj(4);
adj:
+---+---+---+---+
| 0 | 0 | 0 | 0 |
+---+---+---+---+

Here, adj is a single vector containing four initialized elements, all of which are zeros.

 

2)

vector<int> adj[4];
adj:
+-----+     +-----+     +-----+     +-----+
| []  |     | []  |     | []  |     | []  |
+-----+     +-----+     +-----+     +-----+
  [0]         [1]         [2]         [3]
Here, adj is an array containing four individual vectors. At the start, all these vectors are empty. But as you add elements to them, they'll grow:

For example, after:
adj[0].push_back(5);
adj[1].push_back(10);
adj[2].push_back(15);
adj[3].push_back(20);
The visualization might look something like this:
adj:
+-----+     +-----+     +-----+     +-----+
| [5] |     |[10] |     |[15] |     |[20] |
+-----+     +-----+     +-----+     +-----+
  [0]         [1]         [2]         [3]
Each box in the array adj contains a vector, and the numbers within the brackets [ ] represent the contents of that vector.

 

If you perform the operations again:
adj[0].push_back(5);
adj[1].push_back(10);
adj[2].push_back(15);
adj[3].push_back(20);
The adj array will now have two integers in each of its vectors. Here's the visualization:
adj:
+-------+       +-------+       +-------+       +-------+
| [5,5] |       |[10,10]|       |[15,15]|       |[20,20]|
+-------+       +-------+       +-------+       +-------+
  [0]             [1]             [2]             [3]
Each box (i.e., vector) in the array now has two integers. The numbers within the brackets [ ] show the contents of each vector.

 

** vector<vector<int>>  adj(num) vs  vector<int> adj[num];

The construct vector<vector<int>> in C++ is conceptually similar to having an array of vectors, as in vector<int> adj[4];. Both are used to represent a list (or array) where each element is itself another list (or array).

The primary difference is in how they are initialized and resized:

Static Array of Vectors (vector<int> adj[4];)

The outer array has a fixed size (in this case, 4).
Each element of the outer array is a vector which can be resized dynamically.
No need for dynamic memory allocation for the outer array.
Vector of Vectors (vector<vector<int>> adj;)

Both the outer vector and the inner vectors can be resized dynamically.
Memory for both the outer and inner vectors is allocated dynamically.
More flexible because you can easily add more inner vectors or remove them.

Given the initial operations:
adj[0].push_back(5);
adj[1].push_back(10);
adj[2].push_back(15);
adj[3].push_back(20);

If adj was declared as vector<vector<int>> adj(4); (note: initializing with size 4), the structure would look similar to our previous visualizations:

adj:
+-----+     +-----+     +-----+     +-----+
| [5] |     |[10] |     |[15] |     |[20] |
+-----+     +-----+     +-----+     +-----+
And if you push the values again:
adj:
+-------+       +-------+       +-------+       +-------+
| [5,5] |       |[10,10]|       |[15,15]|       |[20,20]|
+-------+       +-------+       +-------+       +-------+

So, while their dynamic resizing properties differ, the conceptual representation (and the way you'd visualize them after adding elements) is the same.

728x90