Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::subrange

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
       
      Defined in header<ranges>
      template<

         std::input_or_output_iterator I,
         std::sentinel_for<I> S= I,
         ranges::subrange_kind K=std::sized_sentinel_for<S, I>?
                                        ranges::subrange_kind::sized:
                                        ranges::subrange_kind::unsized>
          requires(K== ranges::subrange_kind::sized||!std::sized_sentinel_for<S, I>)
      class subrange

         :publicranges::view_interface<subrange<I, S, K>>
      (1)(since C++20)
      Helper concepts
      template<class From,class To>

      concept/*uses-nonqualification-pointer-conversion*/=

         /* see description */;
      (2)(exposition only*)
      template<class From,class To>
      concept/*convertible-to-non-slicing*/=/* see description */;
      (3)(exposition only*)
      1) Thesubrange class template combines together an iterator and a sentinel into a singleview. It modelssized_range whenever the final template parameter issubrange_kind​::​sized (which happens whenstd::sized_sentinel_for<S, I> is satisfied or when size is passed explicitly as a constructor argument).
      2) Determines whetherFrom is convertible toTo withoutqualification conversions. Equivalent to:
      template<class From,class To>concept/*uses-nonqualification-pointer-conversion*/=std::is_pointer_v<From>&&std::is_pointer_v<To>&&!std::convertible_to<std::remove_pointer_t<From>(*)[],std::remove_pointer_t<To>(*)[]>;
      3) Determines whetherFrom is convertible toTo without derived-to-base conversion:
      template<class From,class To>concept/*convertible-to-non-slicing*/=std::convertible_to<From, To>&&!/*uses-nonqualification-pointer-conversion*/<std::decay_t<From>,std::decay_t<To>>;

      Contents

      [edit]Data members

      Member Definition
      constexprboolStoreSize[static]K== ranges::subrange_kind::sized&&
          !std::sized_sentinel_for<S, I>

      (exposition-only static member constant*)
      Ibegin_ an iterator to the beginning of the subrange
      (exposition-only member object*)
      Send_ a sentinel denoting the end of the subrange
      (exposition-only member object*)
      make-unsigned-like-t <std::iter_difference_t<I>>size_
      (present only ifStoreSize istrue)
      the size of the subrange
      (exposition-only member object*)

      [edit]Member functions

      creates a newsubrange
      (public member function)
      converts thesubrange to apair-like type
      (public member function)[edit]
      Observers
      obtains the iterator
      (public member function)[edit]
      obtains the sentinel
      (public member function)[edit]
      checks whether thesubrange is empty
      (public member function)[edit]
      obtains the size of thesubrange
      (public member function)[edit]
      Iterator operations
      advances the iterator by given distance
      (public member function)[edit]
      obtains a copy of thesubrange with its iterator decremented by a given distance
      (public member function)[edit]
      obtains a copy of thesubrange with its iterator advanced by a given distance
      (public member function)[edit]
      Inherited fromstd::ranges::view_interface
      (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]
      gets the address of derived view's data, provided only if its iterator type satisfiescontiguous_iterator
      (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]
      returns the last element in the derived view, provided only if it satisfiesbidirectional_range andcommon_range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns thenth element in the derived view, provided only if it satisfiesrandom_access_range
      (public member function ofstd::ranges::view_interface<D>)[edit]

      [edit]Deduction guides

      [edit]Non-member functions

      obtains iterator or sentinel from astd::ranges::subrange
      (function template)[edit]

      [edit]Helper types

      specifies whether astd::ranges::subrange modelsstd::ranges::sized_range
      (enum)[edit]
      obtains the size of astd::ranges::subrange
      (class template specialization)[edit]
      obtains the type of the iterator or the sentinel of astd::ranges::subrange
      (class template specialization)[edit]

      [edit]Helper templates

      template<class I,class S,ranges::subrange_kind K>
      constexprboolranges::enable_borrowed_range<ranges::subrange<I, S, K>>=true;
      (since C++20)

      This specialization ofranges::enable_borrowed_range makessubrange satisfyborrowed_range.

      [edit]Example

      Run this code
      #include <map>#include <print>#include <ranges> void make_uppercase(char& v){    v+='A'-'a';} void uppercase_transform(std::multimap<int,char>& m,int k){auto[first, last]= m.equal_range(k);for(auto&[_, v]: std::ranges::subrange(first, last))        make_uppercase(v);} int main(){std::multimap<int,char> mm{{4,'a'},{3,'-'},{4,'b'},{5,'-'},{4,'c'}};std::println("Before: {}", mm);    uppercase_transform(mm,4);std::println("After:  {}", mm);}

      Output:

      Before: {3: '-', 4: 'a', 4: 'b', 4: 'c', 5: '-'}After:  {3: '-', 4: 'A', 4: 'B', 4: 'C', 5: '-'}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3470C++20convertible-to-non-slicing might reject qualification conversionsalways accepts them

      [edit]See also

      helper class template for defining aview, using thecuriously recurring template pattern
      (class template)[edit]

      [edit]External links

      Read/write all values of astd::multimap with a given key in C++20 — SO
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/subrange&oldid=182084"

      [8]ページ先頭

      ©2009-2025 Movatter.jp