The C++ Standard Template Library (STL) is a comprehensive collection of template classes and functions that provide solutions to common programming tasks. It is designed to offer high-performance, reusable components that can be easily integrated into C++ programs. By utilizing the STL, developers can focus on the logic of their applications without needing to implement data structures and algorithms from scratch.
The STL is a cornerstone of modern C++ programming, enabling efficient code development and reducing the potential for errors. It includes powerful features for handling data, performing operations, and managing resources, making it an essential tool for any C++ developer.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector numbers = {1, 2, 3, 4, 5};
sort(numbers.begin(), numbers.end());
for (int num : numbers) {
cout << num << " ";
}
return 0;
}In the above example, the vector container stores a list of integers, and the sort algorithm is used to arrange them in ascending order. The STL simplifies the process by providing these ready-made components.
Containers in STL are used to store collections of objects. They provide various data structures like vector, list, set, and map to efficiently manage and access data.
Algorithms are a collection of functions that perform tasks such as searching, sorting, and manipulating data stored in containers. They are designed to work with a wide variety of data types.
Functions in STL include a range of utility functions that perform specific operations on data, such as for_each and transform, making it easier to process collections of data.
Iterators are objects that point to elements within a container and provide a way to traverse through the container. They are essential for accessing and modifying elements in STL containers.
The pair in C++ STL is a utility that allows the storage of two heterogeneous objects as a single unit. This is particularly useful when there is a need to associate two values together, such as when storing key-value pairs. The pair can store values of any data type, and the values can be accessed directly using first and second members.
#include <bits/stdc++.h>
using namespace std;
int main() {
pair <int, string> student;
student.first = 1;
student.second = "Alice";
cout << "Roll No: " << student.first << ", Name: " << student.second << endl;
return 0;
}Roll No: 1, Name: Alice
#include <bits/stdc++.h>
using namespace std;
int main() {
pair <int, string> student;
student.first = 1;
student.second = "Alice";
cout << "Roll No: " << student.first << ", Name: " << student.second << endl;
pair <pair<int, char >, int> complexPair = {{2, 'B'}, 20};
cout << "Roll No: " << complexPair.first.first << ", Section: " << complexPair.first.second;
cout << ", Marks: " << complexPair.second << endl;
return 0;
}Roll No: 1, Name: Alice Roll No: 2, Section: B, Marks: 20
vector is a dynamic array that can automatically resize itself when elements are added or removed.
It provides the same functionalities as an array but with additional features like dynamic resizing.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
for (int i : v) {
cout << i << " ";
}
return 0;
}Expected Output: 1 2 3 4 5
The push_back function adds a new element at the end of the vector.
This is often used to append elements in a loop or based on certain conditions.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
v.push_back(4);
v.push_back(5);
for (int i : v) {
cout << i << " ";
}
return 0;
}Expected Output: 1 2 3 4 5
The front function returns a reference to the first element in the vector.
This is useful when you need to access the starting element without modifying the vector.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
cout << "Front element: " << v.front();
return 0;
}Expected Output: Front element: 10
The back function returns a reference to the last element in the vector.
It’s typically used when the last inserted element needs to be accessed or modified.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
cout << "Back element: " << v.back();
return 0;
}Expected Output: Back element: 30
Vectors support iterators, which allow traversal through the elements in the vector. Iterators behave like pointers and can be used for accessing elements or performing operations on the vector.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << " ";
}
return 0;
}Expected Output: 10 20 30
The for_each loop in C++11 allows for a simpler way to iterate over vectors.
It simplifies the syntax and enhances code readability.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
for (int i : v) {
cout << i << " ";
}
return 0;
}Expected Output: 10 20 30
The erase function removes an element from the vector by a position causing all subsequent elements to be
shifted one position to the left.This helps reduces the size of the vector by one.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
v.erase(v.begin() + 1);
for (int i : v) {
cout << i << " ";
}
return 0;
}Expected Output: 10 30
The insert function inserts elements at a specified position in the vector.
It shifts all elements at and after the insertion point to the right, increasing the vector's size.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 30};
v.insert(v.begin() + 1, 20);
for (int i : v) {
cout << i << " ";
}
return 0;
}Expected Output: 10 20 30
The size function returns the number of elements in the vector.
This is helpful in loops or conditions where the vector’s length is a determining factor.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
cout << "Size of vector: " << v.size();
return 0;
}Expected Output: Size of vector: 3
Elements in a vector can be accessed using the [] operator or the at() method.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
cout << "Element at index 1: " << v[1];
return 0;
}Expected Output: Element at index 1: 20
The swap function swaps the contents of two vectors.
This is an efficient way to swap large vectors since it only swaps the internal pointers rather than copying elements.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v1 = {1, 2, 3};
vector<int> v2 = {4, 5, 6};
swap(v1, v2);
for (int i : v1) {
cout << i << " ";
}
return 0;
}Expected Output: 4 5 6
The clear function removes all elements from the vector, making it empty.
It reduces the vector's size to zero but does not free the memory allocated by the vector.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
v.clear();
cout << "Size after clear: " << v.size();
return 0;
}Expected Output: Size after clear: 0