跳到主要内容

std::multimap end() 方法

// Non const version
iterator end() noexcept;

// Const version
iterator end() const noexcept;

// Const version
const_iterator cend() const noexcept;

返回指向数组末尾之后元素的迭代器

指向数组末尾之后一个元素的迭代器。如果数组为空,返回的迭代器将等于 begin()

尝试解引用末尾之后的迭代器是未定义行为

.

参数

(无)

返回值

指向第一个元素的迭代器。

异常

(无)

复杂度

常数 - O(1)

end 和 cend 的区别

对于 const 容器 c,end 和 cend 是相同的 - c.end() == c.cend()

对于非常量类型c的容器,它们返回不同的迭代器

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

示例

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";
}
输出
{ 8, LONG }
{ 8, long }
{ 4, int }
{ 2, short }
{ 1, char }
mmap = { { 1, char } { 2, short } { 4, int } { 4, float } { 8, long } { 8, double } };
本文来源于 CppReference 上的此页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::multimap end() 方法

// Non const version
iterator end() noexcept;

// Const version
iterator end() const noexcept;

// Const version
const_iterator cend() const noexcept;

返回指向数组末尾之后元素的迭代器

指向数组末尾之后一个元素的迭代器。如果数组为空,返回的迭代器将等于 begin()

尝试解引用末尾之后的迭代器是未定义行为

.

参数

(无)

返回值

指向第一个元素的迭代器。

异常

(无)

复杂度

常数 - O(1)

end 和 cend 的区别

对于 const 容器 c,end 和 cend 是相同的 - c.end() == c.cend()

对于非常量类型c的容器,它们返回不同的迭代器

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

示例

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";
}
输出
{ 8, LONG }
{ 8, long }
{ 4, int }
{ 2, short }
{ 1, char }
mmap = { { 1, char } { 2, short } { 4, int } { 4, float } { 8, long } { 8, double } };
本文来源于 CppReference 上的此页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。