ranges/joinwithview.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 <string>
#include <vector>
#include <array>
#include <ranges>

int main()
{
  std::vector<std::string> rg1{"tic", "tac", "toe"};
  std::vector<std::string> rg2{"none"};
  std::vector<std::string> rg3{"one", "two"};
  std::array coll{rg1, rg2, rg3};

  std::println("coll:  {}", coll);                                // ranges of ranges
  std::println("join:  {}", coll | std::views::join);             // joined
  std::println(" +sep: {}", coll | std::views::join_with("--"));  // with separator
  std::println("       {}", std::views::join_with(coll, "--"));   // with separator
}