Power up C++ with STL

Megha B H
5 min readOct 7, 2021

--

Become a better coder with STL

Many programmers eagerly learn C++ as a language but keep its standard library, the STL, in the last place of learning. Using C++ without greater functionalities means dropping half its strength.

If you are using C++ as your main programming language to solve problems or if you are not a C++ programmer earlier but want to become one because of the great functionality of its libraries, then it is a must for you to know about C++ STLs.
Irrespective of where you’re coming from, this article can help. Here, we will analyze a great tool- Standard Template Library (STL) — that sometimes, can save you a lot of time in programming.

Let’s begin with defining STL

STL in C++ stands for Standard Template Library. It’s a sophisticated and powerful library of template classes and template functions that implement many common data structures and algorithms and forms part of the C++ Standard Library.

It is recommended to have a working knowledge of templates before going deeper into STL, hence this article is not for C++ beginners.

One might ask, why should a C++ programmer be interested in the STL?

It is because STL helps developers to code quickly and efficiently. Quickly in the sense, as most algorithms and data structures are already been implemented in STL, so we just need to use them as required.

Let’s take an example. Let’s say there’s a question that can be easily solved using binary search. But implementing this algorithm itself takes some time, right? But in C++ you can easily code a problem using binary search by just including some header files and that’s it, you are done!

The use of STL can dramatically change your programming style, allowing your code to be more reusable, solid, and robust increasing productivity, by making the developer focus on the main issue. Programming is a way of life and you can make it easier by taking advantage of STL. STL is also extensible, letting you add customized containers and algorithms.

STL Components

Four kinds of STL components

The lucid explanation for STL is to begin from its containers.

Containers

You choose containers when you want to operate with several elements. Right? In native C (not C++) there was only one type of container: the array.

The problem is not that arrays are limited (though, for example, it’s impossible to determine the size of an array at runtime). Rather, the main problem is that many programs require a container with greater functionality.

Let’s say, one or more functionalities are needed for the below operations

  1. Add some elements to a container
  2. Remove an element from a container
  3. Determine whether an element is present in the container
  4. Return count of distinct elements in a container
  5. Iterate through a container and get a list of added elements in some order

Yes, of course, you might think that one can implement this functionality with an array. But the trivial implementation would be very inefficient. You can create the tree- of hash- structure to solve it in a faster way, but think a bit: does the implementation of such a container depend on elements we are going to store? Do we have to re-implement the module to make it functional, for example, for points on a plane but not strings?

If not, we can develop the interface for such a container once, and then use it everywhere for data of any type. That, in short, is the idea of STL containers.

There are four types of containers:

  1. Sequence Containers are used for data structures that store objects of the same type linearly.
  2. Container Adaptors are not full container classes on their own, but wrappers around other container types (such as a vector, deque, or list). These container adapters encapsulate the underlying container type and limit the user interfaces accordingly.
  3. Associative Containers are designed in such a way that they can support direct access to elements using keys. They are not sequential. The associative containers are based on tree structures
  4. Unordered Associative are similar to associative containers but unordered associative containers are based on another form of a data structure called a hash table.

3 things to remember for all the programs using STL

  1. #include the appropriate standard headers: put the name of the container in the title of the standard header and no extension is required. For example, if you are going to use vector, just add the following line at the beginning of your program:
    #include <vector>
  2. Define in special namespace: You need to define container types (and algorithms, functors, and all STL as well) in a special namespace known as “std” as these are not defined in the global namespace. Insert the below line before the code begins and after the header file inclusion:
    using namespace std;
  3. Type of a container is the template parameter: Template parameters are specified with the ‘<’/’>’ “brackets” in code. For example:
    vector < int > N; //for storing integer values
    When using nested structures, make sure that the “brackets” are not directly next to one another — insert a blank between them.
    vector < vector < int > > CorrectDefinition;
    vector < vector < int >> WrongDefinition;
    // Wrong: compiler may be confused by ‘operator >>’

Conclusion

This sums up a brief introduction to Standard Template Library. That’s all you need to get started. Over time you’ll learn the power of using STL effectively. I plan on digging deep and my next blog will focus on vectors, sets, and maps with some hands-on. So, stay connected because you are going to learn many things in the simplest way possible, and as the cherry on top, find an STL cheat sheet at the end.

Lastly, never stop exploring. Learning to use STL is no small task. The C++ standard has a wide range of concepts and methodologies that can be derived from the library. No article can replace the knowledge of a few good books and a lot of implementations.

Drop me a comment if you have any queries or if you want me to write about anything else for you…

Thanks! Signing off!
Megha B. Halasangimath

LinkedIn

--

--

Megha B H

Passionate about coding, imperfectly empowering woman, creating a life I love ❤