Hello Coders,We welcome you all to learn the new concept and improve your knowledge.
If you are a beginner or have some basic knowledge on the vector.We highly recommend you to go through the previous article on the same topic.I’m sure that you all have read our previous article. if not, then we highly recommend you to read it, as it will make your basics of vector concrete.
At present you all have some knowledge on vectors such as how to declare and initialize a vector and its member functions. Today, we will learn what is 2-D vector (it is nothing but a vector of vectors), how to declare and initialize the 2-D vector and the disadvantages of a vector.
Definition:
A 2-D vector is nothing but a vector of vectors.Vector of vectors is a two-dimensional vector where each index of a vector stores a vector.
So let’s see an example:
Output:
1 2 3
4 5 6
--------------------------------------------
1 2 3
4 5 6
--------------------------------------------
1 2
4 5
Note: Each row in a two-dimensional vector is a vector. These vectors can have the same or variable size so while declaring a 2-dimensional vector it is important to handle the size of each inner vector.
Disadvantage of vector:
Vectors are implemented as dynamic arrays.When a vector is declared in the program std::vector allocates the contiguous storage location in heap memory and all its elements at this location .
For example, let’s create a vector of ints i.e. std::vector<int> . Now suppose it’s initial capacity is to store 10 elements, but in our application we want to store 15 elements in it. Then what will happen when we will insert the 11th element?
The current capacity of the vector is 10 as the size of the vector surpasses the capacity. Then to insert a new element in the vector the following steps are performed.
1) It will allocate a bigger chunk of memory on heap i.e. almost double the size of previously allocated.
2) Then it copies all the elements from the old memory location to new one. Yes it copies them, so in case our elements are user defined objects then their copy constructor will be called. Which makes this step quite heavy in terms of speed.
3) Then after successful copying it deletes the old memory.
You can check the current capacity of the vector i.e. how many elements it
can store in current allocated memory using capacity() member function.
To check the count of currently stored elements in std::vector one can use
size() member function.
Let's see the proof of above dicussion:
Output:
Size=1 Capacity=1
Size=2 Capacity=2
Size=3 Capacity=4
Size=4 Capacity=4
Size=5 Capacity=8
Size=6 Capacity=8
Size=7 Capacity=8
Size=8 Capacity=8
Size=9 Capacity=16
Size=10 Capacity=16
In the above program we have declared the vector v of ints and we inserted an element in the end of the vector initial size and capacity of the vector v is 0. As we inserted first element in the vector the computer internally creates an array in heap memory of capacity two and stores 1 in it and size became one .As we insert 3rd element in the vector, a new array will be created of capacity 4 and all the elements of previous array gets copied into it and the new element is added at the end and the size of vector becomes 3 and capacity is 4 .
This process of creating a new array and coping all the items of the older storage location into this new array and deleting the older storage location as soon as the size of the vector hits its maximum value.This process of reallocation of memory makes the insertion/deletion operation inefficient.
Solution:
You can call this function at any time if-only-if you know the maximum number of elements you are going to store in it.
Guess the output of following code:
So that’s it for this article we will be coming up with our next article very soon till then keep learning,
keep coding, keep reading and keep improving !!
Happy Coding 😊
By Programmers Army