std::multiset end() method
- od C++11
- do C++11
// Non const version
iterator end() noexcept;
// Const version
iterator end() const noexcept;
// Const version
const_iterator cend() const noexcept;
// Non const version
iterator end();
// Const version
const_iterator cend();
Returns an iterator
to the element past-the-end of the array. If the array is empty, the returned iterator will be equal tobegin()
.
Attempting to dereference a past-the-end iterator is undefined behaviour
.Parameters
(none)
Return value
Iterator to the first element.
Exceptions
(none)
Complexity
Constant - O(1).
Difference between end and cend
For a const container c
, end and cend are the same - c.end() == c.cend()
For non-const container of type c
they return different iterators:
- Non const container
- Const container
- end
- cend
#include <multiset>
int main()
{
std::multiset<int> multiset = { 1, 2, 3 };
auto it = arr.end(); // Type: std::multiset<int>::iterator
*std::prev(it) = 5; // ✔ Ok
}
#include <multiset>
int main()
{
std::multiset<int> multiset = { 1, 2, 3 };
auto it = arr.cend(); // Type: std::multiset<int>::const_iterator
*std::prev(it) = 5; // ❌ Error!
}
- end
- cend
#include <map>
int main()
{
const std::multiset<int, float> map = { {1, 1.f}, {2, 3.f}, {5, 8.f} };
auto it = arr.end(); // Type: std::multiset<int>::const_iterator
*std::prev(it) = 5; // ❌ Error!
}
#include <map>
int main()
{
const std::multiset<int, float> map = { {1, 1.f}, {2, 3.f}, {5, 8.f} };
auto it = arr.cend(); // Type: std::multiset<int>::const_iterator
*std::prev(it) = 5; // ❌ Error!
}
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
}
}
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.
Hover to see the original license.