跳到主要内容

std::multimap begin() 方法

// Non const version
iterator begin() noexcept;

// Const version
iterator begin() const noexcept;

// Const version
const_iterator cbegin() const noexcept;

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

指向 multimap 的第一个元素。如果数组为空,返回的迭代器将等于 end()

参数

(无)

返回值

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

异常

(无)

复杂度

常数 - O(1)

begin 和 cbegin 的区别

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

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

#include <map>

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

示例

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

int main()
{
std::multimap<std::string, int> multimap {
{ "█", 1 },
{ "▒", 5 },
{ "░", 3 },
{ "▓", 7 },
{ "▓", 8 },
{ "░", 4 },
{ "▒", 6 },
{ "█", 2 },
};

std::cout << "Print out in reverse order using const reverse iterators:\n";
std::for_each(multimap.crbegin(), multimap.crend(),
[](std::pair<const std::string, int> const& e) {
std::cout << "{ \"" << e.first << "\", " << e.second << " };\n";
});

multimap.rbegin()->second = 42; // OK: non-const value is modifiable
// multimap.crbegin()->second = 42; // Error: can't modify the const value
}
可能输出
Print out in reverse order using const reverse iterators:
{ "▓", 8 };
{ "▓", 7 };
{ "▒", 6 };
{ "▒", 5 };
{ "░", 4 };
{ "░", 3 };
{ "█", 2 };
{ "█", 1 };
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而有所修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::multimap begin() 方法

// Non const version
iterator begin() noexcept;

// Const version
iterator begin() const noexcept;

// Const version
const_iterator cbegin() const noexcept;

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

指向 multimap 的第一个元素。如果数组为空,返回的迭代器将等于 end()

参数

(无)

返回值

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

异常

(无)

复杂度

常数 - O(1)

begin 和 cbegin 的区别

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

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

#include <map>

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

示例

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

int main()
{
std::multimap<std::string, int> multimap {
{ "█", 1 },
{ "▒", 5 },
{ "░", 3 },
{ "▓", 7 },
{ "▓", 8 },
{ "░", 4 },
{ "▒", 6 },
{ "█", 2 },
};

std::cout << "Print out in reverse order using const reverse iterators:\n";
std::for_each(multimap.crbegin(), multimap.crend(),
[](std::pair<const std::string, int> const& e) {
std::cout << "{ \"" << e.first << "\", " << e.second << " };\n";
});

multimap.rbegin()->second = 42; // OK: non-const value is modifiable
// multimap.crbegin()->second = 42; // Error: can't modify the const value
}
可能输出
Print out in reverse order using const reverse iterators:
{ "▓", 8 };
{ "▓", 7 };
{ "▒", 6 };
{ "▒", 5 };
{ "░", 4 };
{ "░", 3 };
{ "█", 2 };
{ "█", 1 };
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而有所修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。