Stl
Stl
1. Containers
2. Algorithm
3. Iterators
4. Functors (Function Objects)
Containers
Containers are data structures that are used to store and manage collections of data or
objects such as vectors, lists, sets, and maps.
1. Sequence Containers
They are containers that store elements in a specific linear order. They allow for direct access
to elements and functionalities to manage the order and arrangement of elements.
Array − These are fixed-size collections of elements.
Vector − It's a dynamic array that can resize its size as per requirement.
Deque − Double-ended queue that allows fast insertion and deletion at both ends.
List − A doubly linked list that allows bidirectional iteration.
Forward List − It's a singly linked list that allows efficient insertion and deletion but only
traversal in one direction.
String − In C++ string is also considered a sequential container, it is implemented as a dynamic
array of characters that behaves similarly to other sequential containers.
2. Associative Containers
It stores elements in a sorted order which allows fast retrieval based on keys.
These containers work in a key-value pair structure, where each element is
associated with a unique key. This key is used for quick access to the
corresponding value.
Set − It is a collection of unique elements sorted in a specific order.
Map − It is a collection of key-value pairs, where keys are unique.
Multiset − It is similar to a set, but allows duplicate elements.
Multimap − It is similar to a map, but allows duplicate keys.
3. Unordered Associative Containers
It stores elements in an unordered manner allowing for efficient access, insertion, and
deletion based on keys. Instead of maintaining a sorted order of elements they use hashing to
organize data.
unordered_set − It is a collection of unique elements, without any specific order.
unordered_map − It is a collection of key-value pairs without a specific order.
unordered_multiset − It allows duplicate elements without a specific order.
unordered_multimap − It allows duplicate keys without a specific order.
4. Container Adapters
It provides a different interface for existing containers.
Stack − It is a data structure that follows the Last In, First Out (LIFO) principle.
Queue − It is a data structure that follows the First In, First Out (FIFO) principle.
Priority Queue − It is a special type of queue where elements are removed based on priority.
Algorithms
Algorithms in the C++ Standard Template Library (STL) is a big
collection of functions which is specifically designed to perform
operations on containers.
Where these are implemented using iterators which are used to
traverse containers without the need to know their internal structure.
Non-modifying Sequence Algorithms
for_each − It applies in a function to find out each element in a range.
count − It counts the number of occurrences of a value in a range.
find() − It finds the first occurrence of a value in a range.
find_if − It finds the first element satisfying a predicate.
find_if_not − It finds the first element not satisfying a predicate.
equal − It checks if two ranges are equal.
search − It searches for a subsequence within a sequence.
Modifying Sequence Algorithms
copy() − It copies elements from one range to another.
copy_if − It copies elements satisfying a predicate to another range.
copy_n − It copies a specific number of elements from one range to another.
move − it moves elements from one range to another.
transform() − It applies a function to a range and stores the result.
remove − It removes elements with a specific value from a range.
remove_if − It removes elements satisfying a predicate.
unique − It removes consecutive duplicate elements.
reverse() − It reverses the order of elements in a range.
swap() − It swaps elements.
Sorting Algorithms
sort − It sorts elements in a range.
stable_sort − It sorts elements while maintaining the relative order of
equivalent elements.
partial_sort − It sorts a portion of a range.
nth_element − It partitions the range such that the nth element is in
its final position.
Searching Algorithms
binary_search − It checks if an element exists in a sorted range.
lower_bound − It searches for the first position where an element can
be inserted to maintain order.
upper_bound − It searches for the position of the first element
greater than a specified value.
binary_search − It checks if an element exists in a sorted range.
lower_bound − It finds the first position where an element can be
inserted to maintain order.
upper_bound − It searches for the position of the first element
greater than a specified value.
equal_range − It returns the range of equal elements
Set Algorithms
set_union − It computes the union of two sets.
set_intersection − It computes the intersection of two sets.
set_difference − It computes the difference between two sets.
set_symmetric_difference − It computes the symmetric difference
between two sets.
Numeric Algorithms
accumulate − it computes the sum (or other operations) of a range.
inner_product − It computes the inner product of two ranges.
adjacent_difference − It computes the differences between adjacent
elements.
Partial_sum − it computes the partial sums of a range.
Iterators
Iterators in the C++ Standard Template Library (STL) are objects that act as
pointers to the elements within a container which provides a uniform interface
for accessing and manipulating data. They serve as a bridge between algorithms
and containers.
Input Iterators − They allow read-only access to elements.
Output Iterators − They allow write-only access to elements.
Forward Iterators − They allow reading and writing, which can be incremented.
Bidirectional Iterators − They can be incremented and decremented.
Random Access Iterators − They support arithmetic operations and can access
elements directly.
Function Objects (Functors)
A functor (or function object) in C++ is an object that can be
called as if it were a function. It can be invoked like regular functions.
This capability is achieved by overloading the
function call operator().
1. Arithmetic Functors
In C++, arithmetic operators are used to perform basic mathematical
operations. Here are the common arithmetic operators along with
examples.
Addition (+) − it combines two values to produce their sum.
Subtraction (-) − It calculates the difference between two values.
Multiplication (*) − It multiplies two values to produce their product.
Division (/) − It divides one value by another, resulting in a quotient.
Modulus (%) − It returns the remainder of the division of one value by
another.
Negate () − It returns the negated value of a parameter.
2. Comparison Functors
Comparison Functors are used for comparing values, especially for sorting or
searching in containers.
Less Than (<) − It compares and returns true if the first is less than the second.
Greater Than (>) − It compares and returns true if the first is greater than the
second.
Less Than or Equal To (≤) − It compares and returns true if the first is less than or
equal to the second.
Greater Than or Equal To (≥) − It compares and returns true if the first is greater
than or equal to the second.
Equal To (=) − It compares and returns true if they are equal.
Not Equal To (≠) − It compares and returns true if they are not equal.
3. Logical Functors
Logical functors perform logical operations and can be useful in
scenarios involving boolean logic.
Logical AND Functor (&&) − It returns false if at least one or both of
the two boolean arguments is false else returns true.
Logical OR Functor (||) − It returns true if at least one of the two
boolean arguments or both is true.
Logical NOT Functor (!) − Returns true if the boolean argument is false
and vice versa, it returns the opposite to the provided boolean.
4. Bitwise Functors
bit_and − This performs a bitwise AND operation on two operands,
bit_or − This performs a bitwise OR operation on two operands,
Bit_xor − This performs a bitwise exclusive OR (XOR) operation on two
operands and returns the output corresponding to it.