How do I use std::ranges with existing containers?

In C++, the `` library offers powerful tools to work with a variety of containers, providing a more expressive way to handle collections of data. It allows developers to operate directly on existing containers while leveraging algorithms in a range-oriented fashion.

Here's how you can use std::ranges with a standard container like std::vector.

#include <iostream> #include <vector> #include <ranges> int main() { std::vector numbers = {1, 2, 3, 4, 5}; // Using std::ranges to transform the vector auto squared_numbers = numbers | std::views::transform([](int n) { return n * n; }); for (int n : squared_numbers) { std::cout << n << " "; } return 0; }

C++ std::ranges existing containers std::vector algorithms C++20