# 8 More C++23 Examples

DevFeed: [8 More C++23 Examples](<https://devfeed.tech/articles/8-more-c-23-examples-53305.md>)

Original publisher: [Read original article](<https://www.cppstories.com/2024/cpp23_more_examples/>)

Published: 2024-12-30T00:00:00Z

Content type: tutorial

Language: en

Sources: [C++ Stories](<https://devfeed.tech/sources/c-stories.md>)

Topics: [formatting](<https://devfeed.tech/topics/formatting.md>), [function](<https://devfeed.tech/topics/function.md>), [input](<https://devfeed.tech/topics/input.md>), [file](<https://devfeed.tech/topics/file.md>)

Tags: [examples](<https://devfeed.tech/tags/examples.md>), [file](<https://devfeed.tech/tags/file.md>), [formatting](<https://devfeed.tech/tags/formatting.md>), [functions](<https://devfeed.tech/tags/functions.md>), [input](<https://devfeed.tech/tags/input.md>)

## AI overview

A tutorial presents eight larger examples combining C++23 library features, including ranges-to-container conversion, formatted output, monadic operations, lazy file-reading sequences, and multidimensional data processing.

## Source excerpt

In this article, you'll see eight larger examples that illustrate the changes in C++23. C++23 brings a ton of cool features, as you can see in my two previous articles (here and here). So far, we explored each new addition one by one, but I'd like to share more examples that combine multiple features. Here we go: 1. std::ranges::to<> The ranges::to<> feature allows you to convert ranges into containers easily: #include <ranges> #include <vector> #include <iostream> #include <string> #include <unordered_map> #include <map> int main() { std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::unordered_map<int, std::string> number_to_text = { {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}, {6, "six"}, {7, "seven"}, {8, "eight"}, {9, "nine"}, {10, "ten"} }; auto even_numbers = numbers | std::views::filter([](int n) { return n % 2 == 0; }) | std::ranges::to<std::vector>(); auto text_numbers_map = even_numbers | std::views::transform([&number_to_text](int n) { return std::pair{n, number_to_text.contains(n) ? number_to_text[n] : "unknown" } }) | std::ranges::to<std::map>(); std::cout << "Even numbers: "; for (int n : even_numbers) std::cout << n << " "; std::cout << "\nTextual numbers:\n"; for (const auto& [k, v] : text_numbers_map) std::cout << k << " -> " << v << '\n'; } Run @Compiler Explorer As you can see, there's no issue in creating not only vectors but maps as well. 2. std::print and std::println The new formatted output library simplifies printing. Here's an example that demonstrates alignment, formatting, and table creation: #include <print> #include <unordered_map> #include <string> #include <numeric> int main() { // Store the data in an unordered_map (country -> size in km²) std::unordered_map<std::string, double> country_sizes = { {"USA", 9833517}, {"Canada", 9984670}, {"Australia", 7692024}, {"China", 9596961}, {"Poland", 312696} }; constexpr double KM_TO_MI = 0.386102; // Conversion factor const double total_km = std::accumulate(coun