a table topped with books and headphones

C++ Map Function

In C++, the map function is a powerful tool that allows you to perform operations on each element of a container, such as an array or a vector, and store the results in another container. The map function is part of the Standard Template Library (STL) and provides a convenient way to apply a specific operation to every element in a collection.

The map function takes three arguments: the beginning and ending iterators of the input container, and a function or lambda expression that defines the operation to be performed on each element. The result is a new container with the transformed elements.

Let’s explore the map function with some examples:

Example 1: Squaring Elements


#include 
#include 
#include 

int main() {
    std::vector numbers = {1, 2, 3, 4, 5};
    std::vector squaredNumbers;

    std::transform(numbers.begin(), numbers.end(), std::back_inserter(squaredNumbers),
                   [](int num) { return num * num; });

    for (int num : squaredNumbers) {
        std::cout << num << " ";
    }

    return 0;
}

In this example, we have a vector of numbers and want to square each element. We use the map function, implemented with the std::transform algorithm, to apply the squaring operation to each element. The result is stored in the squaredNumbers vector, which we then print out.

The lambda expression [](int num) { return num * num; } defines the squaring operation. It takes an integer num as input and returns its square.

Example 2: Converting Strings to Uppercase


#include 
#include 
#include 
#include 
#include 

int main() {
    std::vector words = {"hello", "world", "map", "function"};
    std::vector uppercaseWords;

    std::transform(words.begin(), words.end(), std::back_inserter(uppercaseWords),
                   [](std::string word) {
                       std::transform(word.begin(), word.end(), word.begin(), [](unsigned char c) {
                           return std::toupper(c);
                       });
                       return word;
                   });

    for (std::string word : uppercaseWords) {
        std::cout << word << " ";
    }

    return 0;
}

In this example, we have a vector of lowercase words and want to convert them to uppercase. We use the map function, again implemented with the std::transform algorithm, to apply the uppercase conversion operation to each element. The result is stored in the uppercaseWords vector, which we then print out.

The lambda expression [](std::string word) { ... } defines the operation to convert a single word to uppercase. Inside the lambda, we use the std::transform algorithm again, this time to convert each character of the word to uppercase using the std::toupper function.

Example 3: Mapping Custom Objects


#include 
#include 
#include 
#include 

class Person {
public:
    Person(const std::string& name, int age) : name(name), age(age) {}

    std::string getName() const {
        return name;
    }

    int getAge() const {
        return age;
    }

private:
    std::string name;
    int age;
};

int main() {
    std::vector people = {Person("Alice", 25), Person("Bob", 30), Person("Charlie", 35)};
    std::vector names;

    std::transform(people.begin(), people.end(), std::back_inserter(names),
                   [](const Person& person) { return person.getName(); });

    for (std::string name : names) {
        std::cout << name << " ";
    }

    return 0;
}

In this example, we have a vector of custom objects representing people, and we want to extract their names into a separate vector. We use the map function, once again implemented with the std::transform algorithm, to apply the getName function to each person object and retrieve their names. The result is stored in the names vector, which we then print out.

The lambda expression [](const Person& person) { return person.getName(); } defines the operation to extract the name of a person object. It takes a constant reference to a person object as input and returns their name.

These examples demonstrate the versatility of the map function in C++. By providing a concise and expressive way to apply operations to every element of a container, the map function simplifies many common programming tasks.

Scroll to Top