std::unordered_set::begin,(3)
okay so welcome to std::unordered_set::begin,(3), straight out of the C++ Standard Libary manual, bestie β¨π
the manual name is folded right in here so we don't have to copy the header line, iykyk.
π NAME π
std::unordered_set::begin, β yes, the page name is literally std::unordered_set::begin,, and the manual's NAME line says std::unordered_set::begin, - std::unordered_set::begin, with all that comma energy. No cap, that's the whole name entry fr fr.
β¨ Synopsis β¨
Here's the synopsis, served fresh with all three overloads:
iterator begin() noexcept; (1) (since C++11)
const_iterator begin() const noexcept; (2) (since C++11)
const_iterator cbegin() const noexcept; (3) (since C++11)
It returns an iterator to the first element of the unordered_set. π¦
If the unordered_set is empty, the returned iterator will be equal to end() β so yeah, basically the "nothing here" handshake. π
Also there's a diagram reference chillin' right there: range-begin-end.svg π
πΈ Parameters πΈ
(none)β literally no parameters, bestie. None. Zero. The void. π
π Return value π
An iterator to the first element. That's it, that's the gift. π
β‘ Complexity β‘
Constant. That's the whole vibe check β constant, no cap. π
π Notes π
Okay so this is important, bestie: both iterator and const_iterator are constant iterators (and may in fact be the same type). Because of that, it is not possible to mutate the elements of the container through an iterator returned by any of these member functions. So don't try to sneak a mutation in through begin() or cbegin() β the manual says nope, periodt. π
π§ Example π§
Here's the whole example, copied with care because code is sacred:
// Run this code
#include <iostream>
#include <unordered_set>
struct Point { double x, y; };
int main()
{
Point pts[3] = {{1, 0}, {2, 0}, {3, 0}};
// points is a set containing the addresses of points
std::unordered_set<Point*> points = { pts, pts + 1, pts + 2 };
// Change each y-coordinate of (i, 0) from 0 into i^2 and print the point
for (auto iter = points.begin(); iter != points.end(); ++iter)
{
(*iter)->y = ((*iter)->x) * ((*iter)->x); // iter is a pointer-to-Point*
std::cout << "(" << (*iter)->x << ", " << (*iter)->y << ") ";
}
std::cout << '\n';
// Now using the range-based for loop, we increase each y-coordinate by 10
for (Point* i : points)
{
i->y += 10;
std::cout << "(" << i->x << ", " << i->y << ") ";
}
}
π Possible output π
Like, maybe this, depending on the unordered vibe:
(3, 9) (1, 1) (2, 4)
(3, 19) (1, 11) (2, 14)
π¦ See also π¦
endβ returns an iterator to the endcendβ (public member function)begincbeginβ returns an iterator to the beginning of a container or array(C++11)β (function template)(C++14)
mwah π that's the whole page. Source/version: http://cppreference.com, date: 2024.06.10, and the footer tag is std::unordered_set::begin,(3). Bye bestie, go get that first element π«Άπ
π¦ 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