Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::rbegin,std::crbegin

      From cppreference.com
      <cpp‎ |iterator
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      Algorithm concepts and utilities
      Indirect callable concepts
      Common algorithm requirements
      (C++20)
      (C++20)
      (C++20)
      Utilities
      (C++20)
      Iterator adaptors
      Range access
      (C++11)(C++14)
      rbegincrbegin
      (C++14)(C++14)  
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++17)(C++20)
      (C++17)
      (C++17)
       
      Defined in header<array>
      Defined in header<deque>
      Defined in header<flat_map>
      Defined in header<flat_set>
      Defined in header<forward_list>
      Defined in header<inplace_vector>
      Defined in header<iterator>
      Defined in header<list>
      Defined in header<map>
      Defined in header<regex>
      Defined in header<set>
      Defined in header<span>
      Defined in header<string>
      Defined in header<string_view>
      Defined in header<unordered_map>
      Defined in header<unordered_set>
      Defined in header<vector>
      template<class C>
      auto rbegin( C& c)-> decltype(c.rbegin());
      (1)(since C++14)
      (constexpr since C++17)
      template<class C>
      auto rbegin(const C& c)-> decltype(c.rbegin());
      (2)(since C++14)
      (constexpr since C++17)
      template<class T,std::size_t N>
      std::reverse_iterator<T*> rbegin( T(&array)[N]);
      (3)(since C++14)
      (constexpr since C++17)
      template<class T>
      std::reverse_iterator<const T*> rbegin(std::initializer_list<T> il);
      (4)(since C++14)
      (constexpr since C++17)
      template<class C>
      auto crbegin(const C& c)-> decltype(std::rbegin(c));
      (5)(since C++14)
      (constexpr since C++17)

      Returns an iterator to the reverse-beginning of the given range.

      1,2) Returnsc.rbegin(), which is typically an iterator to the reverse-beginning of the sequence represented byc.
      1) IfC is a standardContainer, returns aC::reverse_iterator object.
      2) IfC is a standardContainer, returns aC::const_reverse_iterator object.
      3) Returns astd::reverse_iterator<T*> object to the reverse-beginning ofarray.
      4) Returns astd::reverse_iterator<const T*> object to the reverse-beginning ofil.
      5) Returnsstd::rbegin(c), withc always treated as const-qualified.
      IfC is a standardContainer, returns aC::const_reverse_iterator object.

      range-rbegin-rend.svg

      Contents

      [edit]Parameters

      c - a container or view with arbegin member function
      array - an array of arbitrary type
      il - anstd::initializer_list

      [edit]Return value

      1,2)c.rbegin()
      3)std::reverse_iterator<T*>(array+ N)
      4)std::reverse_iterator<const T*>(il.end())
      5)c.rbegin()

      [edit]Exceptions

      May throw implementation-defined exceptions.

      [edit]Overloads

      Custom overloads ofrbegin may be provided for classes and enumerations that do not expose a suitablerbegin() member function, yet can be iterated.

      Overloads ofrbegin found byargument-dependent lookup can be used to customize the behavior ofstd::ranges::rbegin andstd::ranges::crbegin.

      (since C++20)

      [edit]Notes

      The overload forstd::initializer_list is necessary because it does not have a member functionrbegin.

      [edit]Example

      Run this code
      #include <iostream>#include <iterator>#include <vector> int main(){std::vector<int> v={3,1,4};auto vi= std::rbegin(v);// the type of “vi” is std::vector<int>::reverse_iteratorstd::cout<<"*vi = "<<*vi<<'\n'; *std::rbegin(v)=42;// OK: after assignment v[2] == 42//  *std::crbegin(v) = 13; // error: the location is read-only int a[]={-5,10,15};auto ai= std::rbegin(a);// the type of “ai” is std::reverse_iterator<int*>std::cout<<"*ai = "<<*ai<<'\n'; auto il={3,1,4};// the type of “it” below is std::reverse_iterator<int const*>:for(auto it= std::rbegin(il); it!=std::rend(il);++it)std::cout<<*it<<' ';std::cout<<'\n';}

      Output:

      *vi = 4*ai = 154 1 3

      [edit]See also

      (C++11)(C++14)
      returns an iterator to the beginning of a container or array
      (function template)[edit]
      (C++11)(C++14)
      returns an iterator to the end of a container or array
      (function template)[edit]
      (C++14)
      returns a reverse end iterator for a container or array
      (function template)[edit]
      returns a reverse iterator to a range
      (customization point object)[edit]
      returns a reverse iterator to a read-only range
      (customization point object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/rbegin&oldid=177484"

      [8]ページ先頭

      ©2009-2025 Movatter.jp