std::multis...set::cbegin(3)

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

Hey bestie πŸ’– you're on the std::multis...set::cbegin(3) page from the C++ Standard Libary manual β€” yes, that's exactly how the manual name is spelled here, no cap. This page is all about std::multiset::begin and std::multiset::cbegin, and it's lowkey iconic.

πŸ’– NAME πŸ’–

std::multiset::begin,std::multiset::cbegin - std::multiset::begin,std::multiset::cbegin

That's the whole name block β€” exact function names, exact comma situation, we're not touching it.

✨ Synopsis ✨

Here are the three signatures, verbatim, with their numbered tags and noexcept notes:

          iterator begin();                       (1) (noexcept since C++11)
          const_iterator begin() const;           (2) (noexcept since C++11)
          const_iterator cbegin() const noexcept; (3) (since C++11)

So what does it do? Returns an iterator to the first element of the multiset. If the multiset is empty, the returned iterator will be equal to end().

There's also a diagram called range-begin-end.svg in the original β€” imagine it pointing at the beginning and the end like a little visual vibe check. πŸ¦‹

🫢 Parameters 🫢

(none) β€” literally none. No arguments, no hidden extras, just vibes.

πŸŽ€ Return value πŸŽ€

An Iterator to the first element. That's the whole return value. Slay.

⚑ Complexity ⚑

Constant. Periodt.

🧠 Notes 🧠

Okay important caveat, bestie: because both iterator and const_iterator are 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 try to sneak a change in through begin() or cbegin() β€” not happening. πŸ™ˆ

Also: libc++ backports cbegin() to C++98 mode.

🌈 Example 🌈

Run this code β€” it's giving std::multiset<std::string> realness and shows off the duplicate-key situation:

#include <iostream>
#include <iterator>
#include <set>
#include <string>

int main()
{
    const std::multiset<std::string> words =
    {
        "some", "not", "sorted", "words",
        "will", "come", "out", "sorted",
    };

    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
    }
}

πŸ’… Output πŸ’…

Output is giving alphabetical-with-a-duplicate realness:

come:   1
not:    1
out:    1
some:   1
sorted: 2
will:   1
words:  1

πŸ‘‘ See also πŸ‘‘

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

Source/version/date sign-off, because the footer deserves love too: this page is from http://cppreference.com, dated 2024.06.10, and its page tag is std::multis...set::cbegin(3). Bye bestie, go iterate responsibly πŸ«ΆπŸ’–

πŸ“¦ where this page comes from