STL (standard tag
library)
- KEY COMPONENTS OF C++
- OPERATION SUPPORTED BY ITERATORS
- ADVANTAGES AND DISADVANTAGES OF STL
- C++ STL CONTAINERS
- ALGORITHMS
- FUNCTION OBJECTS
You will discover the Standard Template Library (STL) to be an
invaluable asset in your capacity as an IT project manager. The collection
comprises C++ template classes that provide an extensive array of programming
data structures and operations, such as stacks, arrays, and lists, among
others. It comprises container classes, algorithms, and iterators in their
entirety. Parameterizing the components of the public library guarantees
optimal functionality. It is critical to possess a comprehensive comprehension
of model classes when engaging with STL.
The C++ Standard Template Library (STL) provides an extensive assortment of
algorithms, data structures, and additional components that significantly
optimize the process of C++ program development, akin to the capabilities of an
IT project manager. The STL provides a variety of data manipulation, sorting,
and searching algorithms, in addition to a selection of containers including
vectors, lists, and maps.
An essential advantage of the STL is its capacity to streamline the development
of flexible and reusable code that applies to a variety of data types. By
employing this methodology, a solitary algorithm can be effortlessly
implemented across diverse data types, obviating the necessity for distinct code
for each type. The STL also provides an
efficient way to write code. Many STL algorithms and data structures are
implemented with optimized algorithms that can result in faster execution times
compared to custom code.
Some key components of
STL are basically: -
STL supports a variety of data storage and manipulation containers,
including arrays, vectors, lists, maps, and arrays.
For manipulating data contained in containers, the STL provides a variety of
practical algorithms, such as classification, search, and binary search
algorithms.
Iterators serve as practical instruments for traversing the components of a
container. Iterators within the STL are multipurpose instruments that are
compatible with a wide range of container types. Forward_iterator, bidirectional_iterator,
and random_access_iterator are some examples.
Functional objects, alternatively referred to as functionals, are meta-objects
that can be employed as algorithmic arguments. By allowing the passage of a
function to an algorithm, they afford the capability to customize its behavior.
Adapters are useful components that can modify the behavior of other STL
components. As an expert in data manipulation, one can utilize the
reverse_iterator adapter to alter the arrangement of elements within a
container.
Using STL can help streamline your code, minimize the chances of mistakes, and
enhance program efficiency.
It has a total of 4 components:
-
1) Algorithm
2) Container
3) Functor
4) Iterators
Algorithm
A header algorithm defines a set of functions specifically designed for use with multiple elements. They act on containers and provide resources for various operations on the contents of the containers.
· Algorithm
· Sorting
· Searching
· Important
STL Algorithms
· Useful
Array Algorithms
· Partition
Functions
· Numeric
· Val-array
Class.
Containers
Containers or container classes store objects and data. The standards have a total of seven "first-class" container classes three container adapters, and only seven header files that allow access to those containers or container adapters.
Serialized containers: implement
data structures that can be accessed sequentially.
Vector
List
Deque
Arrays
Forward_list (Introduced in
C++11)
Container adapters: provides a
different interface to sequential containers.
Queue
Priority queue
Stack
Association containers:
implement ordered data structures that can be searched quickly (O(log n)
complexity).
Multiset
Map
Multimap
Unordered associative
containers: implements unordered data structures that can be searched quickly
Unordered_set (introduced in C++
11)
Unordered_multiset (introduced
C++ 11);
Unordered_map (introduced in C++
11)
Unordered_multimap (introduced
in C++ 11).
Functor
The STL contains classes that
overload the function call operator. Instances of such classes are called
function objects or functions. Functors allow you to customize the behavior of
the associated function by using passed parameters. Must Read - Functors.
Iterator
As the name suggests,
iterators are used to process a series of values. They are the most important
feature that allows generality in STL. Must Read – Iterators
Utility Library
Defined in header
<utility>
Iterator categories
Iterator mainly divided
into 5 categories
1) Input
2) output
3) Forward
4) Bi-directional
5) Random
access
Input iterator:
An input iterator is an iterator
that allows a program to read valuesfrom a container.
Dereferencing input iterator
allows reading the value from the container but does not change the value.
The input iterator is a one-way
iterator.
An input iterator can be
incremented but not decremented.
Output iterator:
The output iterator is similar
to the input iterator, except that it allows the program to change the value of
the container, but it does not allow it to be read.
This is a one-way iterator.
This is a writable iterator.
Forward Iterator:
The forward iterator uses the ++
operator to navigate the container.
The previous iterator goes
through each element of the container, one element at a time.
Two-way iterator:
The two-way iterator is similar
to the previous iterator except that it also iterates backward.
This is a two-way iterator.
It can be increased as well as
decreased.
Random Access Iterator:
The Random Access Iterator can
be used to access a random element of a container.
An arbitrary iterator has all
the properties of a two-way iterator with one additional property, namely
pointer incrementing. Using the pointer increment function, we can access a
random element from the container.
Operations supported by iterators
iterator |
Element access |
Read |
Write |
Increment operation |
Comparison |
input |
-> |
v = *p |
++ |
==,!= |
|
output |
*p = v |
++ |
|||
forward |
-> |
v = *p |
*p = v |
++ |
==,!= |
Bidirectional |
-> |
v = *p |
*p = v |
++,-- |
==,!= |
Random access |
->,[ ] |
v = *p |
*p = v |
++,--,+,-,+=,--= |
==,!=,<,>,<=,>= |
Advantages and disadvantages of
STL
The ability of the STL to generate code that is compatible with various data types is one of its primary advantages, as it enables code to be highly reusable. This may produce code that is simpler to maintain and more efficient.
Simplified Test Library (STL) algorithms and data structures are constructed
utilizing algorithms that are exceptionally optimized, resulting in execution
times that are shorter in comparison to custom code.
Enhanced code readability: The STL provides a standardized and thoroughly
documented methodology for manipulating data, leading to the production of code
that is easier to comprehend and maintain.
Because of its wide user base and widespread acceptance, STL has a wealth of
resources like forums and tutorials in addition to a large number of developers
that are ready to help.
Disadvantage
Learning curve: Because STL uses sophisticated features like iterations and function objects, together with a complicated syntax, it can be challenging to master, particularly for novices.
Lack of control: When using
the STL, you must rely on the implementation provided by the library, which can
limit control over certain parts of your code.
· Performance:
In some cases, using STL can result in slower execution times compared to
regular code, especially when dealing with small amounts of data.
Courses are the key to your success
whether you're looking to advance your abilities in an always changing
technological world or getting ready for your first job interview. Our goal in
providing reasonably priced, high-quality material is to gradually accelerate
your growth. Accompany the millions who have already donated; we are here to
extend the same to you.
C++ STL containers
Containers can be described as
objects containing the same type of data. Containers implement various data
structures such as arrays, lists, trees, etc.
The following are containers
that contain information about all containers and the header file associated with them:
Container |
Description |
Header file |
iterator |
vector |
vector is a class that creates a dynamic array allowing insertions and
deletions at the back. |
<vector> |
Random access |
list |
list is the sequence containers that allow the insertions and
deletions from anywhere. |
<list> |
Bidirectional |
deque |
deque is the double ended queue that allows the insertion and deletion
from both the ends. |
<deque> |
Random access |
set |
set is an associate container for storing unique sets. |
<set> |
Bidirectional |
multiset |
Multiset is an associate container for storing non- unique sets. |
<set> |
Bidirectional |
map |
Map is an associate container for storing unique key-value pairs, i.e.
each key is associated with only one value(one to one mapping). |
<map> |
Bidirectional |
multimap |
multimap is an associate container for storing key- value pair, and
each key can be associated with more than one value. |
<map> |
Bidirectional |
stack |
It follows last in first out(LIFO). |
<stack> |
No iterator |
queue |
It follows first in first out(FIFO). |
<queue> |
No iterator |
Priority-queue |
First element out is always the highest priority element. |
<queue> |
No iterator |
Classification of containers
1) Sequence containers
2) Associative
containers
3) Derived
containers
Associative container Set Multiset Map Multimap |
Derived containers Stack Queue Priority_queue |
Sequence container Vector Deque |
ner |
Algorithms
Algorithms are the functions that different containers use to process its contents.
Things to remember:
Algorithms provides about 60
algorithms to perform complex operations. The standard algorithms allow us to
work with two different types of containers at the same time.
Algorithms are not container
member functions but are independent template functions.
Algorithms save a lot of time
and effort.
If we want to use STL
algorithms, we have to add <algorithm> header file in our program.
Function objects
A function object is a function
wrapped in a class to look like an object. A function object extends the
capabilities of a normal function by using object-oriented N functions, such as
generic programming. Therefore, a function object can be said to be a smart
pointer that has many advantages over a normal function.
· The
advantages of function objects over a regular function are:
· Function
objects can have both member functions and member attributes.
· Function
objects can be initialized before they are used.
· Normal
functions can have different types only if the signature is different. Function
objects can have different types, even if the signature is the same.
· Function
objects are faster than a normal function.
A functional object is also known as a functional object. A function object is an object that contains at least one operator () definition. That means that if we declare an object ‘d’ from the class where the function operator () is defined, we can use the object’d’ as a normal function.
Let’s see this with a C++
program: -
#include <iostream>
using namespace std;
class function_object
{
public:
int operator()(int a, int b)
{
return a+b;
}
};
int main()
{
function_object f;
int result = f(5,5);
cout<<"Addition of a and b is : "<<result;
return 0;
}
Output
Addition of a and b is :
10
No comments:
Post a Comment