C++ is a general-purpose and flexible programming language; that’s why you need a library to support the C++ language. C++ STL (standard template library) is a software library for the C++ language that provides a collection of templates representing containers, iterators, algorithms, and function objects. In this tutorial, you will learn about C++ STL in detail.
Generic Programming
You can define generic programming as an approach that focuses on implementing, designing algorithms and data structures in a particular way such that there is no loss of efficiency. It is a way of developing software that maximizes code reuse without compromising performance. An example of generic programming is STL(standard template library) which is a part of the standard library of C++.
C++ STL
STL stands for Standard Template Library. Alexander Stepanov invented it in 1994, and later it was included in the standard library. The standard library consists of a set of algorithms and data structures that were originally part of the C++ Standard template library.
STL helps in storing and manipulating objects, and it makes the program reusable and robust.
Now, understand the components of STL.
Components of STL
STL has four major components:
- Containers
- Iterators
- Algorithms
- Function objects
So, start with containers.
Containers
If you are dealing with many elements, then you need to use some sort of container. The container can be described as objects that are used to store the collection of data. It helps in recreating and implementing complex data structures efficiently.
Now, containers are further classified into three categories:
- Sequence containers: These are used to implement sequential data structures like a linked list, array, etc.
- Associative containers: These are those containers in which each element has a value that is related to a key. They are used to implement sorted data structures, for example, set, multiset, map, etc.
- Containers adapters: Container adapters can be defined as an interface used to provide functionality to the pre-existing containers.
Sequence Container:
- Vectors: Vectors can be defined as a dynamic array or an array with some additional features.
Syntax:
- Deque: Deque is also known as a double-ended queue that allows inserting and deleting from both ends; they are more efficient than vectors in case of insertion and deletion.
Syntax:
- List: List is also the sequential container and allows non-contiguous allocation. It allows insertion and deletion anywhere in the sequence.
Syntax:
Associative Container:
- Set: It is an associative container that is used to store elements that are unique.
Syntax:
- Multiset: This container is similar to that of the set container; the only difference is that it stores non-unique elements.
Syntax:
- Map: Map container contains sets of key-value pairs where each key is associated with one value.
Syntax:
Here, the int is the key type, and the int is the value type.
- Multimap: These containers also store key-value pairs, but unlike maps, they can have duplicate elements.
Syntax:
Container Adapter:
- Stack: Stack follows the last-in, first-out(LIFO) approach that means new elements are added at the end and removed from that end only.
- Queue: Queue follows the first-in, first-out(FIFO) approach, which means new elements are added from the end and removed from the front.
Example:
In the above example, you are using vector functions and some other functions to do some operations. After declaring the vector v, you add the elements inside the vector using the push_back() function and with the help of the loop. After that, you are displaying the size of the vector using the size() function. Now using the resize() function, you are resizing the vector size to 7.
Using the empty() function, you check if the vector is empty; if it’s false, then you will display not empty, and if it is not false, then you will display the vector is empty.
After that, you display all the vector elements using a for loop and functions like begin() and end(), pointing to the first element and the last element, respectively.
Iterators
Iterators are used to access data in the containers, and it helps in traversing through elements of containers using its functions. They can be incremented and decremented as per choice and help in the manipulation of data in the container.
Iterator functions are:
- begin(): This function points the iterator to the first element of the container.
- end(): This function points the iterator to the last element of the container.
Example:
In the above example, you declare the vector using the initializer list and then declare iterator ptr to the vector.
After that, you declare the vector elements using the iterator functions begin(), and end() functions inside the for loop.
Algorithms
In STL, different types of algorithms can be implemented with the help of iterators. Algorithms can be defined as functions applied to the containers and provide operation for the content of the container. for example : sort(), swap(), min(), max() etc.
Types of algorithms:
- Modifying algorithms
- Non-modifying algorithms
- Sorting algorithms
- Searching algorithms
- Numeric algorithms
Example:
In the above example, you have initialized the i and j variable with some values. And then, you have displayed those values; later, you have swapped those values using the swap() function.
Function Objects
A function object is also known as a functor; it is an object of a class that provides a definition for the operator(). Suppose you have declared an object of some class, then you can use that object just like the function, for example:
Here, you have class name C in which operator() is defined. Inside the main function, you have created the object obj and passed a string argument “World”. Here, you have used the object of the class instead of the function to pass the argument.
Conclusion
After reading this tutorial on C++ STL, you would have understood the Standard template library and generic programming and all the components of STL in C++, like containers, iterators, algorithms, in detail. You also learned about function objects along with some examples.
If you are perhaps looking to build a career in software development, do check the Post Graduate Program in Full Stack Development by Simplilearn. It can prove to be the ideal solution to help you build your career in the right way.
Do you have any questions regarding this tutorial on C++ STL? If you do, then please put them in the comments section. We’ll help you solve your queries. To learn more about C++ STL, click on the following link: C++ STL