A vector is a data structure for maintaining a set of elements having a specific data type. Vectors can be considered as arrays having dynamic size. Just like arrays, vectors also store its elements in a contiguous storage locations. That is why, elements in a vector can be accessed using offsets or regular pointers to its elements.
Although vectors can dynamically increase their size, a vector need not allocate extra storage, every time an element is added to it. This is because a vector may have an actual capacity greater than the storage strictly needed to contain its elements. Different libraries can implement different strategies for increased size to balance between memory usage and reallocations.
To use STL built-in vector, you need to include <vector> header.
Code Implementation
//
// main.cpp
// Vector (STL)
//
// Created by Himanshu on 23/09/21.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printIntVector (vector<int> l) {
vector<int>::iterator it;
for (it = l.begin(); it != l.end(); it++) {
cout<<(*it)<<" ";
}
cout<<endl;
}
void printFloatVector (vector<float> l) {
vector<float>::iterator it;
for (it = l.begin(); it != l.end(); it++) {
cout<<(*it)<<" ";
}
cout<<endl;
}
int main () {
// Declaration
vector<float> first, second;
// Initialisation
vector<int> vectorFirst({10, 20, 15});
// vectorSecond is intialised with 3 ints
// having value as 45
vector<int> vectorSecond(3, 45);
cout<<"vectorFirst elements:"<<endl;
printIntVector(vectorFirst);
cout<<"vectorSecond elements:"<<endl;
printIntVector(vectorSecond);
int arr[3] = {100, 101, 102};
vector<int> vectorThird;
vectorThird.insert(vectorThird.begin(), arr, arr+3);
cout<<"vectorThird elements after initialisation using arr:"<<endl;
printIntVector(vectorThird);
cout<<endl;
cout<<"vectorFirst first element: "<<vectorFirst.front()<<endl;
cout<<"vectorFirst last element: "<<vectorFirst.back()<<endl;
vector<int>::iterator it;
//Iterator to vector's first element
it = vectorFirst.begin();
//insert() returns pointer to the (new) first element
it = vectorFirst.insert (it, 2);
cout<<"vectorFirst elements after adding 2 at position (it):"<<endl;
printIntVector(vectorFirst);
//Iterator to 3rd element after vectorFirst.begin()
vectorFirst.insert (it+3, 2, 300);
cout<<"vectorFirst elements after adding 2 elements (value: 300) at position (it+3):"<<endl;
printIntVector(vectorFirst);
vectorFirst.swap(vectorSecond);
cout<<"vectorFirst elements after swap:"<<endl;
printIntVector(vectorFirst);
cout<<"vectorSecond elements after swap:"<<endl;
printIntVector(vectorSecond);
vectorFirst.clear();
cout<<"vectorFirst elements after clear:"<<endl;
printIntVector(vectorFirst);
if (vectorFirst.empty()) {
cout<<"vectorFirst is empty"<<endl;
}
// push_back – adds a new element
// at the end of the vector
first.push_back (1.0);
first.push_back (2.0);
first.push_back (3.0);
first.push_back (4.0);
first.push_back (5.0);
first.push_back (6.0);
cout<<"first vector elements after push_back():"<<endl;
printFloatVector(first);
cout<<"first vector elements after pop_back():"<<endl;
first.pop_back();
printFloatVector(first);
// erase – removes either a single element (position)
// or a range of elements ([first, last))
first.erase(first.begin() + 1, first.end() - 2);
cout<<"first vector elements after removing the elements from 2 to 4 [2..4)"<<endl;
printFloatVector(first);
return 0;
}
Output
vectorFirst elements: 10 20 15 vectorSecond elements: 45 45 45 vectorThird elements after initialisation using arr: 100 101 102 vectorFirst first element: 10 vectorFirst last element: 15 vectorFirst elements after adding 2 at position (it): 2 10 20 15 vectorFirst elements after adding 2 elements (value: 300) at position (it+3): 2 10 20 300 300 15 vectorFirst elements after swap: 45 45 45 vectorSecond elements after swap: 2 10 20 300 300 15 vectorFirst elements after clear: vectorFirst is empty first vector elements after push_back(): 1 2 3 4 5 6 first vector elements after pop_back(): 1 2 3 4 5 first vector elements after removing the elements from 2 to 4 [2..4) 1 4 5
Here’s a working example: C++ Vector [STL]
One thought on “C++ Standard Template Library (STL) – [Vector]”