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.
#include
Vector can be declared by using the vector class followed by datatype and vector object.
vector v1;
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.
#include #include using namespace std; int main() { vector <int> v{1,2,3,4,5}; for(int value:v) cout<<value<<" "; return 0; }
Output:
1 2 3 4 5
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.
#include #include using namespace std; int main() { vector <int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); for(int value:v) cout<<value<<" "; return 0; }
Output:
1 2 3 4 5
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.
#include #include using namespace std; int main() { vector <int> v(5,1); for(int value:v) cout<<value<<" "; return 0; }
Output:
1 1 1 1 1
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.
#include #include using namespace std; int main(){ int n=5; vector<int> v(n); v[0]=1; v[1]=2; v[2]=3; v[3]=4; for(int value:v) cout<<value<<" "; return 0; }
Output:
1 2 3 4 0
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.
#include #include using namespace std; int main(){ vector<int> v1 {1,2,3,4,5}; vector<int> v2 (v1); for(int value:v2) cout<<value<<" "; return 0; }
Output:
1 2 3 4 5
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.
#include #include using namespace std; int main() { int a[] = {1,2,3,4,5}; int length = sizeof(a)/sizeof(a[0]); vector <int> v(a,a+length); for(int value:v) cout<<value<<" "; return 0; }
Output:
1 2 3 4 5
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.
#include #include using namespace std; int main() { vector <int> v(5); fill(v.begin(),v.end(),1); for(int value:v) cout<<value<<" "; return 0; }
Output:
1 1 1 1 1
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.
#include #include using namespace std; template< typename T, size_t N > vector makeVector( const T (&data)[N] ) { return vector(data, data+N); } int main(){ int values[] = { 1,2,3,4,5}; vector<int> array = makeVector(values); for(int value:array) cout<<value<<" "; }
Output:
1 2 3 4 5
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.
Conclusion
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.