A vector is capable of holding multiple data values similar to arrays. However, it is limited to storing object references rather than primitive data types. The concept of storing an object's reference implies that vectors point to the objects that actually contain the data, as opposed to directly storing the data themselves. Unlike arrays, vectors do not require a predefined size during initialization. They possess the ability to dynamically adjust based on the number of object references, facilitated by the container's automatic management of storage. The container maintains an internal copy of alloc, which is responsible for allocating storage throughout the vector's lifespan. Vectors can be easily accessed and traversed using iterators, as they are stored in contiguous memory locations. Additionally, vectors come with built-in safety features that help prevent program crashes, distinguishing them from arrays. While it is possible to reserve space for vectors, this cannot be done for arrays. Unlike arrays, which are not classified as classes, vectors are considered class objects. In vectors, elements can be removed, a functionality not available with arrays.
With the base 'Collection class,' the vector is presented as a template class. The array stands as the fundamental data structure with its unique characteristics. Vectors come equipped with methods & constructors and do not rely on indices. They present the contrary to Arrays, which depend on indices for organization. In this scenario, the initial element is assigned the lowest memory address, while the final element receives the highest memory address. Vectors excel in object insertion and deletion operations, while Arrays are preferred for frequent object retrieval. Arrays are known for their efficient memory usage, whereas Vectors trade off more memory to facilitate dynamic storage expansion. While Vectors may take longer to access elements, this is not the case with Arrays.
There are four ways of initializing a vector in C++ :
- By entering the values one-by-one
- By using an overloaded constructor of the vector class
- By the help of arrays
- By using another initialized vector
Elements within a vector can be individually added by utilizing the 'push_back' method provided by the vector class.
Algorithm
Begin
Declare v of vector type.
Then we call push_back() function. This is done to insert values into vector v.
Then we print "Vector elements: \n".
" for (int a: v)
print all the elements of variable a."
Example -
#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(10);
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
Using an overloaded constructor -
When a vector contains several elements that share identical values, this technique is employed.
By employing a constructor with multiple parameters in the vector class -
This approach is primarily utilized when populating a vector with numerous elements that have identical values.
Algorithm
Begin
First, we initialize a variable say 's'.
Then we have to create a vector say 'v' with size's'.
Then we initialize vector v1.
Then initialize v2 by v1.
Then we print the elements.
End.
Example -
#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] << " \n";
}
return 0;
}
Output
8
8
8
8
8
8
8
8
8
8
8
8
By the help of arrays -
We provide an array as an argument to the constructor of the vector class. This array holds the elements that will populate the vector.
Algorithm -
Begin
First, we create a vector say v.
Then, we initialize the vector.
In the end, print the elements.
End.
Example -
#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] << " \n";
}
return 0;
}
Output
9
8
7
6
5
4
3
2
1
0
Using another initialized vector -
Here, we need to provide the start and finish iterators of a pre-initialized vector to a constructor of a vector class. Subsequently, we create a new vector and populate it with the contents of the original vector.
Algorithm -
Begin
First, we have to create a vector v1.
Then, we have to initialize vector v1 by an array.
Then we initialize vector v2 by v1.
We have to print the elements.
End.
Example -
#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] << " \n";
}
return 0;
}
Output
1
2
3
4
5
6
7
8