Today we’re going to talk about the C++ Standard Template Library also known as the standard library. C++ is a very old high-level programming language favored for its performance and power.
Even though C++ provides the bells and whistles like other high-level programming languages. Like other programming languages, it is not easy to do certain tasks using the core language features. This is why we need a library. C++ comes with a standard library right out of the box. There are other C++ libraries such as C++ Boost and others but they are not considered part of the C++ standard library. At the root of the C++ standard library is the Standard Template Library also referred to as STL.
The C++ Standard Template Library consists of containers and algorithms. There is one more component that ties these two components of the library together which we refer to as Iterators.
What are C++ Standard Template Library Containers?
C++ Standard Template Library containers are data structures that contain data. Data is not very useful unless it can be accessed or manipulated. The containers in of themselves are not able to do much with the data that they hold without the help of the second component of the C++ Standard Template Library this component is known as Algorithms.
What are Algorithms?
Algorithms in the Standard Template Library are logical functions that operate on the data that is stored in the containers or data structures. Operating on this data may sound trivial enough. The reason is that the data stored in the containers usually have different interfaces.
The problem here is compounded when you have several algorithms operating on several data structures. The containers may need to leverage the capabilities of several algorithms. On the other hand, the algorithms may need to operate on the different types of data stored in different containers.
This would be a N*M implementation where N is the number of containers and M is the number of algorithms. To solve this problem, the C++ Standard Template Library provides a module called an Iterator.
What the Iterators do is they hide the interfaces of the containers from the algorithms. The Algorithm only needs to interact with the Iterators and the Iterators interact with a container. This makes the C++ Standard Template Library more modular. This makes it extendable, easy to maintain and promotes code reuse.
Here is a code snippet showing the connection between an STL Container, an Iterator, and an Algorithm.
In C++11 you can simply initialize the vector using an initializer list.
As we proceed with this series we will get into more coding examples.
Reasons why you Should use the C++ Standard Template Library?
- Standardized and guaranteed availability on all platforms
- For more accuracy and fewer buggy programs
- Better code readability and reduced controlled flow.
- It is an example of what good libraries should look and be written like
- Promotes code reuse and reduces the need to reinvent the wheel
- Efficiency. STL is fast, optimized and uses fewer resources
- Expands your knowledge of algorithms and data structures
That’s it. We have looked at the basics of the C++ Standard Template Library by describing it, looking at some C++ code examples and giving you pointers as to why we should be using the C++ Standard Template Library.
Just to add on, I always recommend using libraries be it the standard library or others when building code to improve efficiency and productivity in your software projects.
The next part of this series will dive a little bit deeper and take a look at the Container component of the C++ Standard Template Library. To be precise head on over to the next part in this series on the C++ Standard Template Library and read more on one of the Sequence Containers, the vector.
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.