vishesh_namdev22056
Saturday, 2024-08-03
The Standard Template Library (STL) in C++ is a powerful set of C++ template classes to provide general-purpose classes and functions with templates that implement many popular and commonly used algorithms and data structures like vectors, lists, queues, and stacks.
The STL has four main components:
vector
, deque
, and list
.set
, multiset
, map
, and multimap
.stack
, queue
, and priority_queue
.Here is a simple example that demonstrates the use of an STL container (vector
), an algorithm (sort
), and an iterator:
#include <iostream>
#include <vector> #include <algorithm> int main() { // Create a vector container std::vector<int> numbers = {5, 2, 8, 1, 3}; // Sort the vector using the sort algorithm std::sort(numbers.begin(), numbers.end()); // Use an iterator to print the sorted numbers std::cout << "Sorted numbers: "; for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; }
In this example:
vector<int>
is a sequence container that holds integers.std::sort
is an algorithm that sorts the elements in the container.std::vector<int>::iterator
is used to iterate through the vector and print the elements.