Standard Template Library in C++

Yashvardhan Singh
Catalysts Reachout
Published in
5 min readSep 2, 2022

Standard Template Library is the latest edition in C++. STL provides programmers to store the data effectively, and do manipulation in stored data. These are the general-purpose templates of classes and functions that help in implementing the basic algorithms and data structures like vector, lists, queue, stack, etc.

As a programmer one has to make sure to store data in such a way, that the retrieval and manipulations become easy to resolve this is called a Standard Template Library (STL). Many innovators who intend to bring a massive change in various tech fields are working on designing new algorithms and implementations respectively, and storing data becomes a tough task, eventually, that makes room for great importance about STL in C++ that resolves their queries to large extends.

Containers

  • This helps the programmer to store the data in the listed below containers for further operations on the data.
  • It’s like people standing in the row; with the power in the hands of the executor to manipulate the row in his/her choice and can add more people in the row in any fashion.
  • 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

As a programmer one should be well versed in STL to implement the new algorithms, it eliminates the complex looping structures and helps in resolving the extreme real-world problems for world ease.

--

--