Outils pour utilisateurs

Outils du site


lang:cpp:ranges

Compatibilité

Conversion d'un range vers un vector

#include <algorithm>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
 
inline constexpr auto as_string = [](std::ranges::view auto rng) {
  auto in = rng | std::views::common;
  return std::string(in.begin(), in.end());
};
 
int main() {
  std::string str =
      "Now is the time for all good men to come to the aid of their county.";
  auto rng = str | std::views::split(' ') | std::views::transform(as_string) |
             std::views::common;
  std::vector<std::string> words(rng.begin(), rng.end());
  for (std::string s : words) std::cout << s << "-";
}

How to make a container from a C++20 range Archive du 12/02/2020 le 03/07/2021

std::ranges::copy and std::views::take_while don't want to play together Archive du 25/02/2021 le 03/07/2021

lang/cpp/ranges.txt · Dernière modification : 2021/07/03 13:44 de root