std::multiset::rbegin,(3)
π std::multiset::rbegin,(3) β C++ Standard Library
okay bestie so this is the page for std::multiset::rbegin π and yes, the manual name is C++ Standard Library, we're in the big leagues now. it's all about walking your multiset backwards, main character energy only.
β¨ NAME β¨
std::multiset::rbegin, β literally just std::multiset::rbegin,. no notes, that's the whole name, slay.
πΈ Synopsis πΈ
reverse_iterator rbegin(); (1) (noexcept since C++11)
const_reverse_iterator rbegin() const; (2) (noexcept since C++11)
const_reverse_iterator crbegin() const noexcept; (3) (since C++11)
so there's three of them, one per flavor π«Ά and no cap, overload (1) and (2) are noexcept since C++11, and (3) has been around since C++11 and is noexcept too.
what they do: returns a reverse iterator to the first element of the reversed multiset. iykyk that this corresponds to the last element of the non-reversed multiset. and if the multiset is empty? the returned iterator is equal to rend(). periodt. π
there's a little diagram on the page called range-rbegin-rend.svg β it's giving "reverse iterator range visual," very demure, very helpful. π
π¦ Parameters π¦
(none)
nothing. zero. zip. ngl the least stressful parameter list ever π
β Return value β
Reverse iterator to the first element. that's it, that's the whole vibe β¨
π₯ Complexity π₯
Constant. O(1) energy, we love a cheap call π
π Notes π
- Because both
iteratorandconst_iteratorare constant iterators (and may in fact be the same type), it is not possible to mutate the elements of the container through an iterator returned by any of these member functions. so don't even try, bestie π the container said no. - 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. β οΈ keep that in mind, this is a real caveat, not a drill.
libc++backportscrbegin()to C++98 mode. cute of them honestly π§
π Example π
// Run this code
#include <iostream>
#include <set>
int main()
{
std::multiset<unsigned> rep{1, 2, 3, 4, 1, 2, 3, 4};
for (auto it = rep.crbegin(); it != rep.crend(); ++it)
{
for (auto n = *it; n > 0; --n)
std::cout << "βΌ" << ' ';
std::cout << '\n';
}
}
it's a std::multiset<unsigned> called rep holding {1, 2, 3, 4, 1, 2, 3, 4} β so yeah, duplicates, it's a multiset, that's the whole point π
and then it walks backwards with crbegin() / crend() printing βΌ n times per element. iconic.
π Output π
βΌ βΌ βΌ βΌ
βΌ βΌ βΌ βΌ
βΌ βΌ βΌ
βΌ βΌ βΌ
βΌ βΌ
βΌ βΌ
βΌ
βΌ
look at that staircase π it's literally descending and it's gorgeous.
π 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)
lowkey the whole takeaway:rbegin()gives you the last element as your "first," it'snoexceptsince C++11, it's constant time, and it will not let you mutate anything. you cannot change the elements. i said what i said. π
that's the page, bestie! π«Άπβ¨ this one came from http://cppreference.com, dated 2024.06.10, and it's std::multiset::rbegin,(3) β go forth and iterate backwards, you absolute legend ππ
π¦ 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