more cpp23 lib examples
more cpp23 lib examples
Changes
C++23 gives us plenty of cool features. Below you can find more examples that extend my blog
article - C++23 Library Features and Reference Cards - C++ Stories.
1. std::ranges::to<>
The ranges::to<> feature allows you to easily convert ranges into containers:
#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};
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}
};
// Table headers
std::println("{:<15} | {:>15} | {:>15}", "Country", "Size (km²)", "Size
(mi²)");
std::println("{:-<15}-+-{:-<15}-+-{:-<15}", "", "", ""); // Separator line
// Table rows
for (const auto& [country, size_km] : country_sizes) {
double size_mi = size_km * KM_TO_MI; // Compute size in square miles
dynamically
std::println("{:<15} | {:>15.0f} | {:>15.2f}", country, size_km,
size_mi);
}
// Footer
std::println("{:-<15}-+-{:-<15}-+-{:-<15}", "", "", ""); // Separator line
std::println("{:<15} | {:>15.0f} | {:>15.2f}", "Total", total_km, total_mi);
}
Example output:
Country | Size (km²) | Size (mi²)
----------------+-----------------+----------------
Poland | 312696 | 120732.55
China | 9596961 | 3705405.84
Australia | 7692024 | 2969905.85
Canada | 9984670 | 3855101.06
USA | 9833517 | 3796740.58
----------------+-----------------+----------------
Total | 37419868 | 14447885.87
#include <optional>
#include <iostream>
#include <string>
#include <algorithm>
std::optional<std::string> get_user_input() {
std::string input;
std::cout << "Enter your name: ";
std::getline(std::cin, input);
if (input.empty()) return std::nullopt;
return input;
}
int main() {
auto result = get_user_input()
.transform([](std::string name) {
std::transform(name.begin(), name.end(), name.begin(), ::toupper);
return name;
})
.and_then([](std::string name) {
if (name == "ADMIN") return std::optional<std::string>("Welcome,
Admin!");
return std::optional<std::string>("Hello, " + name + "!");
})
.or_else([] {
return std::optional<std::string>("No input provided.");
});
#include <generator>
#include <fstream>
#include <iostream>
#include <string>
int main() {
const std::string temp_filename = "temp_file.txt";
{
std::ofstream temp_file(temp_filename);
temp_file << "Line 1: Hello, World!\n";
temp_file << "Line 2: This is a test.\n";
temp_file << "Line 3: C++ coroutines are cool!\n";
}
5. std::mdspan
The std::mdspan feature is useful for multidimensional data. Here's an example of matrix
multiplication.
#include <https://raw.githubusercontent.com/kokkos/mdspan/single-
header/mdspan.hpp>
#include <vector>
#include <print>
int main() {
std::vector<int> A_data = {1, 2, 3, 4, 5, 6};
std::vector<int> B_data = {1, 2, 3, 4, 5, 6};
std::vector<int> C_data(4);
multiply_matrices(A, B, C);
int main() {
std::vector<int> range1 = {1, 2, 3};
std::vector<char> range2 = {'A', 'B', 'C'};
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};