Przejdź do głównej zawartości

std::unordered_multiset begin()/cbegin() method

// Non const version
iterator end() noexcept;

// Const version
const_iterator end() const noexcept;

// Const version
const_iterator cend() const noexcept;

Returns an iterator

to the element past-the-end of the unordered_multiset. If the unordered_multiset is empty, the returned iterator will be equal to begin().

uwaga

Attempting to dereference a past-the-end iterator is undefined behaviour

.

Parameters

(none)

Return value

Iterator to the first element.

Complexity

Constant - O(1).

Difference between begin and cbegin

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

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

#include <unordered_map>
#include <string>

int main()
{
std::unordered_multiset<std::string> multiset = { "abc", "Hello World!", "abc", "asd" };
auto it = multiset.end(); // Type: std::unordered_multiset<std::string>::iterator
std::prev(it)->second = 5; // ✔ Ok
}

Example

Main.cpp
#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 cnt = words.count(*it);
std::cout << *it << ":\t" << cnt << '\n';
std::advance(it, cnt); // all cnt elements have equivalent keys
}
}
Possible Output
some:   1
words: 2
to: 1
count: 2
these: 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::unordered_multiset begin()/cbegin() method

// Non const version
iterator end() noexcept;

// Const version
const_iterator end() const noexcept;

// Const version
const_iterator cend() const noexcept;

Returns an iterator

to the element past-the-end of the unordered_multiset. If the unordered_multiset is empty, the returned iterator will be equal to begin().

uwaga

Attempting to dereference a past-the-end iterator is undefined behaviour

.

Parameters

(none)

Return value

Iterator to the first element.

Complexity

Constant - O(1).

Difference between begin and cbegin

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

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

#include <unordered_map>
#include <string>

int main()
{
std::unordered_multiset<std::string> multiset = { "abc", "Hello World!", "abc", "asd" };
auto it = multiset.end(); // Type: std::unordered_multiset<std::string>::iterator
std::prev(it)->second = 5; // ✔ Ok
}

Example

Main.cpp
#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 cnt = words.count(*it);
std::cout << *it << ":\t" << cnt << '\n';
std::advance(it, cnt); // all cnt elements have equivalent keys
}
}
Possible Output
some:   1
words: 2
to: 1
count: 2
these: 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.