Introduction to STL in C++

Tutorial 3 of 5

Introduction to STL in C++

1. Introduction

In this tutorial, we will explore the Standard Template Library (STL) in C++, an important feature of the language that is often used but rarely understood in depth.

The goal of this tutorial is to provide a clear understanding of the STL, its components, and how to use them effectively. By the end of this tutorial, you should be able to understand the key elements of the STL and employ them in your C++ code.

Prerequisites: Basic knowledge of C++ programming is required. Understanding of templates in C++ would be beneficial but is not mandatory.

2. Step-by-Step Guide

The STL is a powerful library in C++ that provides generic, template-based classes and functions to implement common data structures and algorithms. It consists of four main components:

  • Containers: These are data structures that store data. They include vector, list, deque, set, map, etc.

  • Algorithms: These are common functions for computation on data. They include sort, search, modify, etc.

  • Iterators: These are used to step through the elements of containers. They can be thought of as a generalized form of pointers.

  • Function Objects: These are objects that can be used as functions. They are also known as functors.

Practical examples will help us understand these better.

3. Code Examples

Example 1: Using Vector (Container)

#include <iostream>
#include <vector>

int main() {
    // Create a vector of integers
    std::vector<int> vec;

    // Add elements to the vector
    vec.push_back(10);
    vec.push_back(20);
    vec.push_back(30);

    // Use an iterator to print the elements
    for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}

In the above code, we create a vector of integers and add some elements to it. We then use an iterator to go through the elements and print them. The expected output is 10 20 30.

Example 2: Using Sort (Algorithm)

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    // Create a vector of integers
    std::vector<int> vec = {30, 10, 20};

    // Sort the vector
    std::sort(vec.begin(), vec.end());

    // Print the sorted vector
    for(int i : vec) {
        std::cout << i << " ";
    }

    return 0;
}

In this code, we create a vector and then use the sort algorithm from the STL to sort the elements. The expected output is 10 20 30.

4. Summary

In this tutorial, we've covered the basics of the STL in C++, including containers, algorithms, iterators, and function objects. As next steps, you can explore more about each of these components, and also look into some of the other STL components like allocators and adaptors.

5. Practice Exercises

  1. Create a std::list of integers and use an iterator to print the elements.

  2. Create a std::set of strings, add some elements, and print them using a for-each loop.

  3. Use the std::find algorithm to find an element in a std::vector.

Solutions:

  1. cpp std::list<int> lst = {10, 20, 30}; for(std::list<int>::iterator it = lst.begin(); it != lst.end(); ++it) { std::cout << *it << " "; }

  2. cpp std::set<std::string> st = {"Hello", "World"}; for(const auto& str : st) { std::cout << str << " "; }

  3. cpp std::vector<int> vec = {10, 20, 30}; if(std::find(vec.begin(), vec.end(), 20) != vec.end()) { std::cout << "Element found"; } else { std::cout << "Element not found"; }