Advanced STL Usage

Tutorial 5 of 5

Advanced STL Usage


1. Introduction

This tutorial aims to guide you through the advanced features of the Standard Template Library (STL) in C++. The STL provides a set of common classes for C++, such as containers and associative arrays, which can be used with any built-in type and with any user-defined type that supports some elementary operations.

By following this tutorial, you will learn:
- How to use advanced STL functions
- The process of extending STL components
- The underlying principles of STL

Before getting started, make sure you have a basic understanding of C++ programming and a general knowledge of STL, including its primary containers and algorithms.

2. Step-by-Step Guide

Understanding Iterators

Iterators in STL serve as a link between containers and algorithms. They are objects that point to an element in a container. STL provides several types of iterators, including Input, Output, Forward, Bidirectional, and Random Access.

Advanced Algorithms

STL has a broad set of algorithms. Here we'll focus on some less used but useful ones such as nth_element, partial_sort, next_permutation, etc.

Extending STL

Though STL is extensive, there could be scenarios where you might need to extend it. This can be done by creating a function object, and using it with STL algorithms.

3. Code Examples

Example: Using nth_element

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

int main() {
    std::vector<int> v = {5, 6, 4, 3, 2, 6, 7, 9, 3};

    std::nth_element(v.begin(), v.begin() + v.size()/2, v.end());

    std::cout << "The median is " << v[v.size()/2] << "\n";
}

Here, nth_element is used to rearrange the vector such that the element at the nth position is the one that would be in that position in a sorted sequence. The elements before the nth position are less than the nth element, and those after the nth position are greater.

Example: Extending STL

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

// function object to check if a number is prime
class IsPrime {
public:
    bool operator()(int n) {
        if (n <= 1) return false;
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) return false;
        }
        return true;
    }
};

int main() {
    std::vector<int> v = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

    int primeCount = std::count_if(v.begin(), v.end(), IsPrime());

    std::cout << "Found " << primeCount << " prime numbers.\n";
}

4. Summary

In this tutorial, we explored advanced STL usage, including understanding iterators and advanced algorithms. We also learned how to extend the STL by creating a function object.

You can further explore STL by understanding different types of containers, algorithms, and function objects in more detail.

5. Practice Exercises

  1. Write a program that uses STL's next_permutation to generate all permutations of a string.
  2. Write a function object that checks if a string is a palindrome. Use it with STL's count_if to count the number of palindromes in a vector of strings.

Note: A palindrome is a word that reads the same backward as forward. E.g., "madam".

Suggested solutions can be found at cplusplus.com. Experiment with the code, try out different inputs, and understand how the code behaves.