std::unorde...ltiset::end,(3)
πΈ 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()(andcend()) give you a placeholder position, not a real element. Dereferencing it is undefined behavior, no cap. Use it for comparisons likeit != 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
- 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