std::unordered_set::begin,(3)

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

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 end
  • cend β€” (public member function)
  • begin
  • cbegin β€” 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