A vector can store multiple data values, similar to an array, but they can only store object references and not basic data types. Storing object references means they point to the object that contains the data rather than directly storing the data. Unlike arrays, vectors do not require size initialization. They can dynamically resize based on the number of object references, as their storage is managed automatically by the container.
The container will maintain an internal copy of the allocator used to allocate storage space for its entire lifecycle. Vectors can be located and traversed using iterators, which is why they are placed in contiguous storage space. Vectors also have safety features that can prevent program crashes, which arrays do not have. We can provide reserved space for vectors, but we cannot do so for arrays. Arrays are not a class, but vectors are. Elements can be removed from vectors, but not from arrays.
Using the parent class ‘Collection class’, vectors are sent in the form of a template class. Arrays are lower-level data structures with specific properties. Vectors have functions and constructors that are not index-based. They are the opposite of index-based arrays. Here, the lowest address is given to the first element, and the highest address is given to the last element. Vectors are used for inserting and deleting objects, while arrays are used for frequent access to objects. Arrays are a memory-saving data structure, while vectors use more memory to manage storage and grow dynamically. Accessing elements in a vector takes more time, which is not the case for arrays.
There are four ways to initialize a vector in C++:
-
Input values one by one
-
Using the overloaded constructor of the vector class
-
Using the help of an array
-
Using another initialized vector
👇 Click to Claim 👇
👉 C Language Knowledge Resource Collection
Input Values One by One
You can insert all elements into the vector one by one using the vector class’s push_back() method.
Algorithm
Start by declaring a vector variable v. Then call the push_back() function to insert values into vector v. Then print "Vector elements: ". For (int a: v), print all elements of variable a.
Code
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(6);
vec.push_back(7);
vec.push_back(8);
vec.push_back(9);
vec.push_back(101);
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
return 0;
}
Output
Using Overloaded Constructors
This method is used when the vector has multiple elements with the same value.
Using the overloaded constructor of the vector class –
This method is primarily used to fill the vector with multiple elements of the same value.
Algorithm
Start by initializing a variable s. Then create a vector v with size 's'. Then initialize vector v1. Then initialize v2 using v1. Then print the elements. End.
Code
#include <iostream>
#include <vector>
using namespace std;
int main() {
int elements = 12;
vector<int> vec(elements, 8);
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
return 0;
}
Output
888888888888
Using the Help of an Array
Pass an array to the constructor of the vector class. The array contains the elements that will fill the vector.
Algorithm
Start by creating a vector v. Then initialize the vector. Finally, print the elements. End.
Code
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vectr{9,8,7,6,5,4,3,2,1,0};
for (int i = 0; i < vectr.size(); i++) {
cout << vectr[i] << " ";
}
return 0;
}
Output
9876543210
Using Another Initialized Vector
In this method, we need to pass the begin() and end() iterators of the initialized vector to the constructor of the vector class. Then we initialize a new vector and fill it with the old vector.
Algorithm
Start by creating a vector v1. Then initialize vector v1 with an array. Then initialize vector v2 using v1. We will print the elements. End.
Code
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec_1{1,2,3,4,5,6,7,8};
vector<int> vec_2(vec_1.begin(), vec_1.end());
for (int i = 0; i < vec_2.size(); i++) {
cout << vec_2[i] << " ";
}
return 0;
}
Output
1 2 3 4 5 6 7 8
Popular Recommendations
-
Already listed 26 companies.
-
CLion Tutorial – CLion File Templates
-
C Language Algorithm – “Filling Each Node’s Next Right Node Pointer II” Algorithm Problem