Przejdź do głównej zawartości

std::multimap end() method

// Non const version
iterator end() noexcept;

// Const version
iterator end() const noexcept;

// Const version
const_iterator cend() const noexcept;

Returns an iterator

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

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:

#include <map>

int main()
{
std::multimap<int, float> map = { {1, 1.f}, {2, 3.f}, {5, 8.f} };
auto it = arr.end(); // Type: std::multimap<int, float>::iterator
*std::prev(it) = 5; // ✔ Ok
}

Example

Main.cpp
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <string>
#include <cstddef>

int main()
{
auto show_node = [](const auto& node, char ending = '\n') {
std::cout << "{ " << node.first << ", " << node.second << " }" << ending;
};

std::multimap<std::size_t, std::string> mmap;
assert(mmap.begin() == mmap.end()); // OK
assert(mmap.cbegin() == mmap.cend()); // OK

mmap.insert({ sizeof(long), "LONG" });
show_node(*(mmap.cbegin()));
assert(mmap.begin() != mmap.end()); // OK
assert(mmap.cbegin() != mmap.cend()); // OK
mmap.begin()->second = "long";
show_node(*(mmap.cbegin()));

mmap.insert({ sizeof(int), "int" });
show_node(*mmap.cbegin());

mmap.insert({ sizeof(short), "short" });
show_node(*mmap.cbegin());

mmap.insert({ sizeof(char), "char" });
show_node(*mmap.cbegin());

mmap.insert({{ sizeof(float), "float" }, { sizeof(double), "double" }});

std::cout << "mmap = { ";
std::for_each(mmap.cbegin(), mmap.cend(), [&](const auto& n) { show_node(n, ' '); });
std::cout << "};\n";
}
Output
{ 8, LONG }
{ 8, long }
{ 4, int }
{ 2, short }
{ 1, char }
mmap = { { 1, char } { 2, short } { 4, int } { 4, float } { 8, long } { 8, double } };
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::multimap end() method

// Non const version
iterator end() noexcept;

// Const version
iterator end() const noexcept;

// Const version
const_iterator cend() const noexcept;

Returns an iterator

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

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:

#include <map>

int main()
{
std::multimap<int, float> map = { {1, 1.f}, {2, 3.f}, {5, 8.f} };
auto it = arr.end(); // Type: std::multimap<int, float>::iterator
*std::prev(it) = 5; // ✔ Ok
}

Example

Main.cpp
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <string>
#include <cstddef>

int main()
{
auto show_node = [](const auto& node, char ending = '\n') {
std::cout << "{ " << node.first << ", " << node.second << " }" << ending;
};

std::multimap<std::size_t, std::string> mmap;
assert(mmap.begin() == mmap.end()); // OK
assert(mmap.cbegin() == mmap.cend()); // OK

mmap.insert({ sizeof(long), "LONG" });
show_node(*(mmap.cbegin()));
assert(mmap.begin() != mmap.end()); // OK
assert(mmap.cbegin() != mmap.cend()); // OK
mmap.begin()->second = "long";
show_node(*(mmap.cbegin()));

mmap.insert({ sizeof(int), "int" });
show_node(*mmap.cbegin());

mmap.insert({ sizeof(short), "short" });
show_node(*mmap.cbegin());

mmap.insert({ sizeof(char), "char" });
show_node(*mmap.cbegin());

mmap.insert({{ sizeof(float), "float" }, { sizeof(double), "double" }});

std::cout << "mmap = { ";
std::for_each(mmap.cbegin(), mmap.cend(), [&](const auto& n) { show_node(n, ' '); });
std::cout << "};\n";
}
Output
{ 8, LONG }
{ 8, long }
{ 4, int }
{ 2, short }
{ 1, char }
mmap = { { 1, char } { 2, short } { 4, int } { 4, float } { 8, long } { 8, double } };
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.