ranges/fromrange3.cpp

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. Creative Commons License

// raw code

#include <print>
#include <vector>
#include <list>
#include <map>
#include <ranges>

int main()
{
  std::list<std::pair<int, std::string>> pairs{{7, "Rio"}, {1, "LA"}, {2,"NY"}};

  std::map<std::size_t, std::string> map1{std::from_range, pairs};
  std::println("map1: {}", map1);

  std::map map2{std::from_range, pairs};
  std::println("map2: {}", map2);

  std::vector<std::string> vec{"tic", "tac", "toe"};

  std::map<int, std::string> map3{std::from_range, vec | std::views::enumerate};
  std::println("map3: {}", map3);

  std::map<int, std::string> map4{std::from_range, std::views::enumerate(vec)};
  std::println("map4: {}", map4);

  std::map<int, std::string> map5{std::from_range,
                                  std::views::zip(std::views::iota(1), vec)};
  std::println("map5: {}", map5);
}