The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <print>
#include <vector>
#include <set>
#include <array>
#include <ranges>
int main()
{
std::vector<int> coll{47, 11, 0, 8, 15, -11, 7};
std::println("coll: {}", coll);
auto set1 = std::ranges::to<std::set>(coll);
std::println("set1: {}", set1);
std::println("to(): {}", std::ranges::to<std::set>(coll));
auto set2 = std::ranges::to<std::set<double>>(coll);
std::println("set2: {::.2f}", set2); // print doubles with precision of 2 digits
}