Page Content

Tutorials

What Is Standard Template Library In C++ With Code Example

Standard Template Library in C++

The C++ Standard Library is based on and includes the Standard Template Library (STL). The breadth of the language was greatly increased when it was created by Meng Lee and Alexander Stepanov and then incorporated into the C++ standard.

The STL provides generic, type-independent components that developers can utilize in a range of applications without having to write code for each data type. Classes and functions are created using templates, which are formulas or blueprints. Templates optimize efficiency and minimize errors brought on by repetitive encoding by enabling the compiler to produce the required code for certain types at compile time.

Key Components of the STL

Essential Elements of the STL Three fundamental components make up the majority of the STL, which together offer pre-made fixes for typical programming issues:

  1. Containers
  2. Algorithms
  3. Iterators

Containers

There are various methods for managing and storing groups of objects in memory using these templated classes. They provide access, deletion, and insertion actions, and memory is frequently dynamically allocated during runtime. Common STL containers consist of:

Sequence Containers: vector, array, forward_list, list, and deque. These arrange the elements according to insertion.

Associative Containers: set, multiset, multimap, and map. They use a key to store the elements and grant access to them.

Unordered Containers: Unsorted_set, Unsorted_multiset, Unsorted_map, and Unsorted_multimap. These lookups are faster with hashing.

Container Adapters: stack, priority_queue, and queue. In order to provide certain interfaces (such as FIFO or LIFO), these cover other underlying containers.

Algorithms

Usually provided by containers, these type-independent function templates carry out standard operations on sequence members. They are made to work with data manipulation iterators. Min, max, sort, locate, count, copy, and merge are a few examples. You may find the STL algorithm library in the header.

Iterators

An abstraction or generalization of pointers, iterators are frequently called the “glue” of the STL. Like pointers in arrays, they enable algorithms to browse and retrieve elements within containers in a consistent manner. Iterators are used to implement common pointer operators.

The STL comprises additional supporting parts in addition to these three primary components, such as allocators (which control memory for containers) and function objects (classes that define the call operator operator(), which is frequently used to modify algorithm behaviour).

Code Example

The following C++ program exhibits the use of STL components, notably std::vector (a container) and std::sort (an algorithm), together with iterators, after demonstrating basic input/output using iostream (a component of the C++ Standard Library):

#include <iostream>  // Provides stream-oriented I/O facilities like std::cout and std::endl 
#include <vector>    // Provides the std::vector class template, a dynamic array container 
#include <algorithm> // Provides various algorithms, including std::sort
#include <string>    // Provides the std::string class for character sequences
// Using a 'using directive' to avoid prefixing standard library elements with 'std::' for brevity.
// All names defined by the standard library are in the 'std' namespace.
using namespace std;
int main() {
    // Part 1: Basic Input/Output with iostream
    // 'cout' is the standard output stream object.
    // 'endl' is a manipulator that inserts a newline character and flushes the output buffer.
    cout << "Hello, C++ Standard Library and STL!" << endl;
    // Part 2: Demonstrating STL Containers, Algorithms, and Iterators
    // Declare a 'vector' of integers. 'vector' is a class template.
    // This line creates an instance of 'vector' specialized to hold 'int' elements,
    // initialized with a list of values (a C++11 feature).
    vector<int> numbers = {5, 2, 8, 1, 9, 4, 7, 3, 6};
    cout << "Original numbers: ";
    // Use a range-based for loop (a C++11 feature) to iterate over the vector and print its elements.
    for (int num : numbers) {
        cout << num << " ";
    }
    cout << endl;
    // Use a standard algorithm: 'std::sort'.
    // 'sort' is a function template that reorders elements in a range.
    // 'numbers.begin()' returns an iterator to the first element.
    // 'numbers.end()' returns an iterator to the position one past the last element.
    // These iterators define the sequence range for the algorithm.
    sort(numbers.begin(), numbers.end()); 
    cout << "Sorted numbers: ";
    for (int num : numbers) {
        cout << num << " ";
    }
    cout << endl;
    // The 'string' class is another important part of the C++ Standard Library,
    // providing robust ways to handle variable-length character sequences.
    string greeting = "This demonstrates the power of generic programming!";
    cout << greeting << endl;
    return 0; // The 'main' function returns 0 to indicate successful execution.
}

Output

Hello, C++ Standard Library and STL!
Original numbers: 5 2 8 1 9 4 7 3 6 
Sorted numbers: 1 2 3 4 5 6 7 8 9 
This demonstrates the power of generic programming!

You can also read What Is Generic Programming In C++ With Code Examples

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index