Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::views::split,std::ranges::split_view

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
       
      Defined in header<ranges>
      template<ranges::forward_range V,ranges::forward_range Pattern>

      requiresranges::view<V>&&
               ranges::view<Pattern>&&
               std::indirectly_comparable<ranges::iterator_t<V>,
                                         ranges::iterator_t<Pattern>,
                                         ranges::equal_to>
      class split_view

         :publicranges::view_interface<split_view<V, Pattern>>
      (1)(since C++20)
      namespace views{

         inlineconstexpr/* unspecified */ split=/* unspecified */;

      }
      (2)(since C++20)
      Call signature
      template<ranges::viewable_range R,class Pattern>

          requires/* see below */

      constexprranges::viewauto split( R&& r, Pattern&& pattern);
      (since C++20)
      template<class Pattern>
      constexpr/* range adaptor closure */ split( Pattern&& pattern);
      (since C++20)
      1)split_view takes aview and a delimiter, and splits theview into subranges on the delimiter.
      2)RangeAdaptorObject. The expressionviews::split(e, p) isexpression-equivalent tosplit_view(e, p) for any suitable subexpressionse andp.

      split_view models the conceptsforward_range, andcommon_range when the underlyingviewV models respective concepts.

      The inner range (ranges::range_reference_t<split_view>) is aranges::subrange<ranges::iterator_t<V>>, which modelscommon_range, modelssized_range whenranges::iterator_t<V> modelsstd::sized_sentinel_for<ranges::iterator_t<V>>, and modelscontiguous_range,random_access_range,bidirectional_range, andforward_range whenV models respective concepts.

      Unlikelazy_split_view,split_view maintains the continuity of the subrange, making it suitable for string splitting.

      Contents

      [edit]Data members

      Member Description
      Vbase_(private) the underlying (adapted)view
      (exposition-only member object*)
      Patternpattern_(private) the pattern object that is used as a delimiter to split the underlyingview
      (exposition-only member object*)
      non-propagating-cache<ranges::subrange
          <ranges::iterator_t<V>>>
      cached_begin_(private)
      an object that caches the result of the first call tobegin()
      (exposition-only member object*)

      [edit]Member functions

      constructs asplit_view
      (public member function)[edit]
      returns a copy of the underlying (adapted) view
      (public member function)[edit]
      returns an iterator to the beginning
      (public member function)[edit]
      returns an iterator or a sentinel to the end
      (public member function)[edit]
      searches for the next occurrence of the pattern
      (exposition-only member function*)
      Inherited fromstd::ranges::view_interface
      returns whether the derived view is empty, provided only if it satisfiessized_range orforward_range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      (C++23)
      returns a constant iterator to the beginning of the range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      (C++23)
      returns a sentinel for the constant iterator of the range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns whether the derived view is not empty, provided only ifranges::empty is applicable to it
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns the first element in the derived view, provided if it satisfiesforward_range
      (public member function ofstd::ranges::view_interface<D>)[edit]

      [edit]Nested classes

      the iterator type
      (exposition-only member class*)
      the sentinel type
      (exposition-only member class*)

      [edit]Deduction guides

      [edit]Notes

      BeforeP2210R2,split_view used alazy mechanism for splitting, and thus could not keep the bidirectional, random access, or contiguous properties of the underlying view, or make the iterator type of the inner range same as that of the underlying view. Consequently, it is redesigned byP2210R2, and the lazy mechanism is moved tolazy_split_view.

      The delimiterpattern generally should not be an ordinary string literal, as it will consider the null terminator to be necessary part of the delimiter; therefore, it is advisable to use astd::string_view literal instead.

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <ranges>#include <string_view> int main(){using std::operator""sv;constexprauto words{"Hello^_^C++^_^20^_^!"sv};constexprauto delim{"^_^"sv}; for(constauto word: std::views::split(words, delim))// with string_view's C++23 range constructor:std::cout<<std::quoted(std::string_view(word))<<' ';std::cout<<'\n';}

      Output:

      "Hello" "C++" "20" "!"

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P2210R2C++20the oldsplit_view was too lazy to be easily usedit is redesigned

      [edit]See also

      aview over the subranges obtained from splitting anotherview using a delimiter
      (class template)(range adaptor object)[edit]
      aview consisting of the sequence obtained from flattening aview ofranges
      (class template)(range adaptor object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/split_view&oldid=179889"

      [8]ページ先頭

      ©2009-2025 Movatter.jp