C++ Language



Admission Enquiry Form

  

STL(Standard Template Library) in C++:


What is STL(Standard Template Library) in C++ ?

STL in C++ stands for Standard Template Library .It contains template classes which are used to store different types of objects.The STL implements the data structures in C++.STL is added into C++ Latest.But before learning this we should have the knowledge of Template Classes.STL contains the number of container classes.

Features of STL:

  • STL is a Standard Template Library in C++.
  • It is helpful in implementing popular and commonly used algorithms and data structures.
  • It is a powerful set of C++ template classes.
  • STL contains different pre-defined functions which helps you performing various complicated tasks in very easy manner.
  • At the C++ Standard Template Library ,there are three well-structured components (containers, algorithms and iterators).


Some Common Containers

1. Vector
2. Queue
3. Stack
5. List
6. Set
7. Map




Vector in C++

In C++, vectors are used to store elements of similar data types.A vector is class that implements dynamic array, means size automatically changes.

We can change the size of the vector during the execution of a program according to our requirements.
Vectors are part of the C++ Standard Template Library. If we want to use vector , we need to include the vector header file in our program.

#include <iostream>
#include <vector>
using namespace std;

int main()
{

// initializer list
vector<int> vector1 = {1, 2, 3, 4, 5};

// uniform initialization
vector<int> vector2{11,12,13,14,15};

// Third way
//int vector with size 4 and initializes the vector
// with the value of 10. So, the vector is equivalent to
//vector<int> vector3 = {10,10,10,10};

vector<int> vector3(4, 10);

cout << "vector1 = ";

// ranged loop
for (int i : vector1)
{
cout << i << " ";
}

cout << "\nvector2 = ";

// ranged loop
for (int i : vector2)
{
cout << i << " ";
}

cout << "\nvector3 = ";

// ranged loop
for (int i : vector3)
{
cout << i << " ";
}

// adding the integers 10 and 20 to the vector1
vector1.push_back(10);
vector1.push_back(20);

cout << "\nUpdated Vector1: ";

for (int i : vector1) {
cout << i << " ";
}

//Accessing Elements of a Vector1
cout << "\nElement at Index 0: " << vector1.at(0) << endl;
cout << "Element at Index 3: " << vector1.at(3) << endl;
cout << "Element at Index 6: " << vector1.at(6);

//we can also use the square brackets [] to access vector elements.
cout << "\nElement at Index 5: " <<vector1[5];
/* However, the at() function is preferred over []
because at() throws an exception
whenever the vector is out of bound, while [] gives a garbage value.

// gives garbage value
cout<<endl<<"Garbage value "<<vector2[6];

// throws an exception
cout<<endl<<"Throwing exception "<<vector2.at(6)<<endl;
*/

//Changing Vector Element
// change elements at indexes 1 and 3
vector3.at(1) = 19;
vector3.at(3) = 50;

cout << "\nUpdated Vector3: ";

for (int i : vector3)
{
cout << i << " ";
}

//Deleting Elements from C++ Vectors
// remove the last element from the vector
vector2.pop_back();

cout << "\nUpdated Vector2 after deletion of last element: ";
for (int i : vector2)
{
cout << i << " ";
}

return 0;
}

C++ Vector Functions

In C++, the vector header file provides various functions that can be used to perform different operations on a vector.


Function

Description

size()

returns the number of elements present in the vector

clear()

removes all the elements of the vector

front()

returns the first element of the vector

back()

returns the last element of the vector

empty()

returns 1 (true) if the vector is empty

capacity()

check the overall size of a vector