Are you looking to understand the vector concept but finding it difficult to understand. Don't worry we will get you covered with the vector initializing concept. Let us gain insights into the different methods to initialize a vector in C++.
What is a vector?
Vectors are similar to arrays but their size can grow dynamically. Vectors can expand or shrink dynamically during the insertion and deletion of elements. It stores the elements in contiguous memory locations. Hence the size of the vector changes during the execution of the program according to the requirements. Vectors are defined in the standard template library (STL). To use a vector, first, we need to include the vector header in the program.
Vector can be declared by using the vector class followed by datatype and vector object.
How to Initialize Vector in C++
In vectors, the reference of the objects is stored, it does not store the data as it is. Vector can store data of similar types. Defining the size of the vector during vector initializing is exceptional. As it can adjust its size during the run time. We can initialize the vector in many ways. Let us get started with some of the common ways to get a better understanding.
1) By initializing like arrays
In this method, we can initialize the values as we initialize for arrays. We have to give the values between the curly brackets.
2) Using push_back() method
The push_back method is used to insert the elements one by one into the vector. It can add only one element and only at the end of the vector. So for initializing multiple elements we have to call the push_back method many times.
3) Passing overloaded constructor
The constructor accepts two parameters. The first parameter is the size of the vector and the second parameter is the element to be initialized. It initializes all the elements upto the given size with the same value we initialize.
4) Using index position
In this method after declaring the vector, we can initialize each element by assigning value to it by index. The vector object name is followed by the index enclosed in square brackets(obj_name[index]). The values we didn't initialize will be filled with zeros.
5) Initializing by copying another vector
We can easily initialize a vector by creating a copy of another vector. The vector will have the same values as the original vector. This can be done by passing the original vector object as an argument to the new vector object.
6) Copying from an array
Vector can be initialized by copying the values from the arrays also. We have to create an array and initialize the values. Then we have to pass two arguments to the vector object. One is the array, and the other argument is the array with its length.
7) Using the fill method
We can also create an array using the fill method. For that, we need to pass the parameter values beginning of the vector, end of the vector, and the value to insert. Since our vector is initially empty only the value we pass is initialized up to the size of the vector.
8) Using template
A template is an important concept in C++. We can initialize a vector by creating a template and passing a value into the function.
Advantages of vector in C++
- Size varies according to the requirements dynamically.
- It is stored in contiguous memory.
Disadvantages of vector in C++
- A vector is an object so memory consumption is more.
- ArrayList is faster than vectors since they are unsynchronised.
We have covered some of the various methods to initialize the vector in C++. You can choose to use any of the methods according to your requirements. Vectors are one of the most important concepts in STL. So gaining good knowledge Keep practicing more vector concepts will help you to improve your proficiency in C++. If you need help with your c++ homework then our experts are available 24/7.