std::unorde...ltiset::end,(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

std::unordered_multiset::end β€” okay so this is literally the member function that hands you an iterator pointing past the last element of your unordered_multiset πŸ’–

(this is from the C++ Standard Library, std::unordered_multiset::end, in case the header at the top didn't already spill the tea β˜•)


✨ Synopsis ✨

iterator end() noexcept;              (1) (since C++11)
const_iterator end() const noexcept;  (2) (since C++11)
const_iterator cend() const noexcept; (3) (since C++11)

Three whole flavors, bestie, and every single one of them is noexcept and has been serving since C++11 πŸ‘‘

So what does it actually do? It returns an iterator to the element following the last element of the unordered_multiset. Like, the seat right after the last person in the row πŸͺ‘

And here's the part where you need to pay attention: that element acts as a placeholder. The way that attempting to access it results in undefined behavior?? Yeah. Do not. πŸ™ˆ Not me, not you, not ever.

range-begin-end.svg


πŸŽ€ Parameters πŸŽ€

(none)

Literally takes zero arguments. Iconic behavior, no notes. πŸ’…


πŸ¦‹ Return value πŸ¦‹

An iterator to the element following the last element. That's it, that's the whole vibe ✨


πŸ”₯ Complexity πŸ”₯

Constant. O(1) baby, it ate and left no crumbs 🌈


πŸ’œ Notes πŸ’œ

Okay this is important so listen up 🫢 β€” both iterator and const_iterator are constant iterators (and honestly they may in fact be the same type, iykyk). Which means: it is not possible to mutate the elements of the container through an iterator returned by any of these member functions.

So like… don't try to change stuff through it. You can't. The container said no πŸ’€


🌸 Example 🌸

// Run this code

#include <iostream>
#include <iterator>
#include <string>
#include <unordered_set>

int main()
{
    const std::unordered_multiset<std::string> words =
    {
        "some", "words", "to", "count",
        "count", "these", "words"
    };

    for (auto it = words.begin(); it != words.end(); )
    {
        auto count = words.count(*it);
        std::cout << *it << ":\t" << count << '\n';
        std::advance(it, count); // all count elements have equivalent keys
    }
}

⭐ Possible output ⭐

some:   1
words:  2
to:     1
count:  2
these:  1

(ngl, std::advance(it, count) in that loop? main character energy πŸ‘‘ because all count elements have equivalent keys β€” that's the trick πŸ”₯)


🌈 See also 🌈

  • begin, cbegin β€” returns an iterator to the beginning (public member function) πŸ’–
  • end, cend β€” returns an iterator to the end of a container or array (C++11) (function template) (C++14) πŸ¦‹

Fr fr though: end() (and cend()) give you a placeholder position, not a real element. Dereferencing it is undefined behavior, no cap. Use it for comparisons like it != words.end() and nothing else πŸ™ˆ

okay that's the whole page, bestie β€” go slay some iteration loops πŸ’…βœ¨

Signed off with love from http://cppreference.com, dated 2024.06.10, on the std::unorde...ltiset::end,(3) page πŸŽ€πŸ«Ά

πŸ“¦ where this page comes from