Introduction to STL Algorithms

Lesson Introduction

Welcome to the introduction to Standard Template Library (STL) algorithms in C++! In today's fast-paced development environment, efficiency and maintainability are crucial. Using tools like STL algorithms can help you achieve these goals by providing pre-built, robust functions.

By the end of this lesson, you will:

  1. Understand what STL algorithms are and why they are beneficial.
  2. Learn the usage and syntax of the std::for_each algorithm.
  3. See how std::for_each can be used in a practical example.

Introduction to STL Algorithms

STL algorithms are a collection of functions provided by the Standard Template Library (STL) in C++. They perform common operations on sequences of data. Using STL algorithms saves time and effort by leveraging well-tested and optimized operations for tasks like searching, sorting, and transforming data. Why Use STL Algorithms?

  • Efficiency: STL algorithms are highly optimized and can make your code faster.
  • Readability: They offer a clear and expressive way to handle data structures.
  • Reusability: These algorithms have undergone extensive testing, ensuring reliability.

Some commonly used STL algorithms include:

  • std::for_each: Applies a function to a range of elements.
  • std::sort: Sorts a range of elements.
  • std::find: Searches for a value in a range of elements.

In this lesson, we will focus on std::for_each.

Understanding `std::for_each`

std::for_each lets you execute a specified function on every element within a range. Here is its basic signature:

C++
std::for_each(InputIterator first, InputIterator last, Function fn);
  • InputIterator first: An iterator pointing to the start of the range.
  • InputIterator last: An iterator pointing to one past the end of the range.
  • Function fn: A function or function object (often a lambda) to apply to the elements in the range.

This function is especially useful when you need to perform operations on all elements of a container, such as displaying, modifying, or accumulating values.

Example of `std::for_each`

Here’s a basic example demonstrating the usage of std::for_each to print elements of a vector:

C++
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> data = {1, 2, 3, 4, 5};

    // Applying std::for_each
    std::for_each(data.begin(), data.end(), [](int n) {
        std::cout << n << ' ';
    });
    std::cout << '\n';  // Output: 1 2 3 4 5

    return 0;
}

Explanation of the Code:

  1. Initialization: A std::vector is initialized with values {1, 2, 3, 4, 5}.
  2. Calling std::for_each: We call std::for_each with three arguments:
    • data.begin(): An iterator pointing to the beginning of the vector.
    • data.end(): An iterator pointing to one past the end of the vector.
    • Lambda function: This lambda function takes an integer n and prints it, followed by a space.

When std::for_each is executed, the lambda function is called for each element, resulting in the elements being printed to the console.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal