Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::ref_view

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

          requiresstd::is_object_v<R>
      class ref_view

         :publicranges::view_interface<ref_view<R>>
      (since C++20)

      ref_view is aview of the elements of some otherrange. It wraps a reference to thatrange.

      Contents

      [edit]Data members

      Member Description
      R*r_ a pointer to the underlying range
      (exposition-only member object*)

      [edit]Member functions

      constructs aref_view that references to the given range
      (public member function)
      returns the references to the referenced range
      (public member function)
      returns the beginning iterator of the referenced range
      (public member function)
      returns the sentinel of the referenced range
      (public member function)
      checks whether the referenced range is empty
      (public member function)
      returns the size of the referencedsized_range
      (public member function)
      returns the approximate size of the referencedapproximately_sized_range
      (public member function)
      returns the pointer to the beginning of the referencedcontiguous_range
      (public member function)
      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]
      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]

      std::ranges::ref_view::ref_view

      template</*different-from*/<ref_view> T>

          requiresstd::convertible_to<T, R&>&&
                       requires{ _FUN(std::declval<T>());}

      constexpr ref_view( T&& t);
      (since C++20)

      Initializesr_ withstd::addressof(static_cast<R&>(std::forward<T>(t))).

      /*different-from*/<T, U> is satisfied if and only ifstd::remove_cvref_t<T> andstd::remove_cvref_t<U> are not the same type, and overloads of_FUN are declared asvoid _FUN(R&);void _FUN(R&&)= delete;.

      Parameters

      t - range to reference

      std::ranges::ref_view::base

      constexpr R& base()const;
      (since C++20)

      Returns*r_.

      std::ranges::ref_view::begin

      constexprranges::iterator_t<R> begin()const;
      (since C++20)

      Returnsranges::begin(*r_ ).

      std::ranges::ref_view::end

      constexprranges::sentinel_t<R> end()const;
      (since C++20)

      Returnsranges::end(*r_ ).

      std::ranges::ref_view::empty

      constexprbool empty()const
          requires requires{ranges::empty(*r_);};
      (since C++20)

      Returnsranges::empty(*r_ ).

      std::ranges::ref_view::size

      constexprauto size()const
          requiresranges::sized_range<R>;
      (since C++20)

      Returnsranges::size(*r_ ).

      std::ranges::ref_view::reserve_hint

      constexprauto size()const
          requires ranges::approximately_sized_range<R>;
      (since C++26)

      Returnsranges::reserve_hint(*r_ ).

      std::ranges::ref_view::data

      constexprauto data()const
          requiresranges::contiguous_range<R>;
      (since C++20)

      Returnsranges::data(*r_ ).

      [edit]Deduction guides

      template<class R>
      ref_view( R&)-> ref_view<R>;
      (since C++20)

      [edit]Helper templates

      template<class T>
      constexprbool enable_borrowed_range<ranges::ref_view<T>>=true;
      (since C++20)

      This specialization ofstd::ranges::enable_borrowed_range makesref_view satisfyborrowed_range.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_ranges_reserve_hint202502L(C++26)ranges::approximately_sized_range andreserve_hint

      [edit]Example

      Run this code
      #include <iostream>#include <ranges> int main(){conststd::string s{"cosmos"}; const std::ranges::take_view tv{s,3};const std::ranges::ref_view rv{tv}; std::cout<<std::boolalpha<<"call empty(): "<< rv.empty()<<'\n'<<"call size() : "<< rv.size()<<'\n'<<"call begin(): "<<*rv.begin()<<'\n'<<"call end()  : "<<*(rv.end()-1)<<'\n'<<"call data() : "<< rv.data()<<'\n'<<"call base() : "<< rv.base().size()<<'\n'// ~> tv.size()<<"range-for   : "; for(constauto c: rv)std::cout<< c;std::cout<<'\n';}

      Output:

      call empty(): falsecall size() : 3call begin(): ccall end()  : scall data() : cosmoscall base() : 3range-for   : cos

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P2325R3C++20default constructor was provided as
      view must bedefault_initializable
      removed along with the requirement

      [edit]See also

      CopyConstructible andCopyAssignable reference wrapper
      (class template)[edit]
      aview with unique ownership of somerange
      (class template)[edit]
      aview that includes all elements of arange
      (alias template)(range adaptor object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/ref_view&oldid=182372"

      [8]ページ先頭

      ©2009-2025 Movatter.jp