Przejdź do głównej zawartości

std::multiset begin() method

// Non const version
iterator begin() noexcept;

// Const version
iterator begin() const noexcept;

// Const version
const_iterator cbegin() const noexcept;

Returns an iterator

to the first element of the multiset. If the array is empty, the returned iterator will be equal to end().

Parameters

(none)

Return value

Iterator to the first element.

Exceptions

(none)

Complexity

Constant - O(1).

Difference between begin and cbegin

For a const container c, begin and cbegin are the same - c.begin() == c.cbegin()

For non-const container of type c they return different iterators:

#include <multiset>

int main()
{
std::multiset<int> multiset = { 1, 2, 3 };
auto it = multiset.begin(); // Type: std::multiset<int>::iterator
*it = 5; // ✔ Ok
}

Example

Main.cpp
#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 cnt = words.count(*it);
std::cout << *it << ":\t" << cnt << '\n';
std::advance(it, cnt); // all cnt elements have equivalent keys
}
}
Possible output
come:	1
not: 1
out: 1
some: 1
sorted: 2
will: 1
words: 1
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::multiset begin() method

// Non const version
iterator begin() noexcept;

// Const version
iterator begin() const noexcept;

// Const version
const_iterator cbegin() const noexcept;

Returns an iterator

to the first element of the multiset. If the array is empty, the returned iterator will be equal to end().

Parameters

(none)

Return value

Iterator to the first element.

Exceptions

(none)

Complexity

Constant - O(1).

Difference between begin and cbegin

For a const container c, begin and cbegin are the same - c.begin() == c.cbegin()

For non-const container of type c they return different iterators:

#include <multiset>

int main()
{
std::multiset<int> multiset = { 1, 2, 3 };
auto it = multiset.begin(); // Type: std::multiset<int>::iterator
*it = 5; // ✔ Ok
}

Example

Main.cpp
#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 cnt = words.count(*it);
std::cout << *it << ":\t" << cnt << '\n';
std::advance(it, cnt); // all cnt elements have equivalent keys
}
}
Possible output
come:	1
not: 1
out: 1
some: 1
sorted: 2
will: 1
words: 1
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.