C++ Multimap 库 - cbegin() 函数
描述
C++ 函数 std::multimap::cbegin() 返回一个常量迭代器,它引用多重映射的第一个元素。
该成员函数获得的迭代器可以用来迭代容器,但不能用来修改它所指向的对象的内容,即使对象本身不是常量。
声明
以下是 std::multimap::cbegin() 函数形式 std::map 头的声明。
C++11
const_iterator cbegin() const noexcept;
参数
None
返回值
返回常量迭代器。
异常
此成员函数从不抛出异常。
时间复杂度
常数,即 O(1)
示例
以下示例显示了 std::multimap::cbegin() 函数的用法。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
/* Multimap with duplicates */
multimap<char, int> m {
{'a', 1},
{'a', 2},
{'b', 3},
{'c', 4},
{'c', 5},
};
cout << "Multimap contains following elements:" << endl;
for (auto it = m.cbegin(); it != m.cend(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Multimap contains following elements − a = 1 a = 2 b = 3 c = 4 c = 5

