Learn C++ features with digital flashcards!

ranges

Create the following vector std::vector<int> ints{0,1,2,3,4,5};

Use ranges (filter, transform…) to square the even numbers and print them out.

(Example taken from cppreference)

Solution

#include <vector>
#include <ranges>
#include <iostream>
 
int main()
{
    std::vector<int> ints{0,1,2,3,4,5};
    auto even = [](int i){ return 0 == i % 2; };
    auto square = [](int i) { return i * i; };
 
    for (int i : ints | std::views::filter(even) | std::views::transform(square)) {
        std::cout << i << ' ';
    }
}

Links

int main() { return 0; }
Got It No Clue