Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::split_view<V,Pattern>::split_view

      From cppreference.com
      <cpp‎ |ranges‎ |split view
       
       
      Ranges library
      Range adaptors
       
       
      Defined in header<ranges>
      split_view()

          requiresstd::default_initializable<V>&&

                   std::default_initializable<Pattern>=default;
      (1)(since C++20)
      constexprexplicit split_view( V base, Pattern pattern);
      (2)(since C++20)
      template<ranges::forward_range R>

          requiresstd::constructible_from<V,views::all_t<R>>&&
                   std::constructible_from<Pattern,ranges::single_view<
                                                       ranges::range_value_t<R>>>

      constexprexplicit split_view( R&& r,ranges::range_value_t<R> e);
      (3)(since C++20)

      Constructs asplit_view.

      Letbase_ be the underlying view andpattern_ be the delimiter.

      1) Default constructor.Value-initializesbase_ andpattern_ with their default member initializers respectively.
      2) Initializesbase_ withstd::move(base) andpattern_ withstd::move(pattern).
      3) Initializesbase_ withviews::all(std::forward<R>(r)) andpattern_ withranges::single_view{std::move(e)}.

      Contents

      [edit]Parameters

      base - the view (to be split)
      pattern - view to be used as the delimiter
      e - element to be used as the delimiter

      [edit]Example

      Run this code
      #include <algorithm>#include <array>#include <cctype>#include <iostream>#include <iterator>#include <ranges>#include <string_view>#include <vector> int main(){{auto view= std::views::iota(1,20)| std::views::transform([](int x){return x%5;});auto splits= std::views::split(view,0);// (2)for(constauto& split: splits){std::cout<<"{ ";            std::ranges::copy(split,std::ostream_iterator<int>(std::cout," "));std::cout<<"} ";}}std::cout<<'\n'; {conststd::vector nums{1,-1,-1,2,3,-1,-1,4,5,6};conststd::array delim{-1,-1};auto splitter= std::views::split(nums, delim);// (3)for(constauto& split: splitter){std::cout<<"{ ";            std::ranges::copy(split,std::ostream_iterator<int>(std::cout," "));std::cout<<"} ";}}std::cout<<'\n'; {constexprstd::string_view JupiterMoons{"Callisto, Europa, Ganymede, Io, and 91 more"};constexprstd::string_view delim{", "};        std::ranges::split_view moons_extractor{JupiterMoons, delim};// (3)auto is_moon= std::views::filter([](auto str){returnstd::isupper(str[0]);});std::cout<<"Some moons of Jupiter: ";for(constauto moon: moons_extractor| is_moon)std::cout<<std::string_view(moon)<<' ';}std::cout<<'\n';}

      Output:

      { 1 2 3 4 } { 1 2 3 4 } { 1 2 3 4 } { 1 2 3 4 }{ 1 } { 2 3 } { 4 5 6 }Some moons of Jupiter: Callisto Europa Ganymede Io

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3714
      (P2711R1)
      C++20the multi-parameter constructor was not explicitmade explicit

      [edit]See also

      constructs alazy_split_view
      (public member function ofstd::ranges::lazy_split_view<V,Pattern>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/split_view/split_view&oldid=183315"

      [8]ページ先頭

      ©2009-2025 Movatter.jp