std::vector...or::crbegin(3)
okay bestie, this is the std::vector...or::crbegin(3) page from the C++ Standard Libary manual. yes, the header is a little cursed (vector...or), but the energy is immaculate β itβs giving reverse-iterator main character energy π
β¨
π NAME π
std::vector::rbegin,std::vector::crbeginβ with the little-after itstd::vector::rbegin,std::vector::crbeginβ yep, it says it again, and weβre keeping it
π Synopsis π
hereβs the exact signature menu, annotations and all π«Ά
reverse_iterator rbegin(); (1) (noexcept since
C++11)
(constexpr since
C++20)
const_reverse_iterator rbegin() const; (2) (noexcept since
C++11)
(constexpr since
C++20)
const_reverse_iterator crbegin() const noexcept; (3) (since C++11)
(constexpr since
C++20)
so what does it do? it returns a reverse iterator to the first element of the reversed vector. that maps to the last element of the non-reversed vector. if the vector is empty, the returned iterator is equal to rend().
thereβs even a diagram file named range-rbegin-rend.svg β slay π¦
π Parameters π
(none) β literally none. no parameters, no stress π«Ά
π¦ Return value π¦
Reverse iterator to the first element. thatβs the whole return value, bestie.
πΈ Complexity πΈ
Constant. no scaling nonsense, just constant π
β¨ Notes β¨
the underlying iterator of the returned reverse iterator is the end iterator. hence the returned iterator is invalidated if and when the end iterator is invalidated. thatβs a real caveat, okay? keep it in mind π
also: libc++ backports crbegin() to C++98 mode. iconic behavior, ngl π
π Example π
hereβs the exact example, copied with love:
// Run this code
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
int main()
{
std::vector<int> nums{1, 2, 4, 8, 16};
std::vector<std::string> fruits{"orange", "apple", "raspberry"};
std::vector<char> empty;
// Print vector.
std::for_each(nums.rbegin(), nums.rend(), [](const int n) {
std::cout << n << ' '; });
std::cout << '\n';
// Sums all integers in the vector nums (if any), printing only the
result.
std::cout << "Sum of nums: "
<< std::accumulate(nums.rbegin(), nums.rend(), 0) <<
'\n';
// Prints the first fruit in the vector fruits, checking if there
is any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.rbegin() << '\n';
if (empty.rbegin() == empty.rend())
std::cout << "vector 'empty' is indeed empty.\n";
}
π Output π
16 8 4 2 1
Sum of nums: 31
First fruit: raspberry
vector 'empty' is indeed empty.
π See also π
rendβ returns a reverse iterator to the endcrendβ (public member function)(C++11)rbeginβ returns a reverse iterator to the beginning of a container or arraycrbeginβ (function template)(C++14)
byeeee bestie π«Ά this pageβs source/version is http://cppreference.com, dated 2024.06.10, and the footer tag is std::vector...or::crbegin(3). mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms