C++ 映射库 - end() 函数



描述

C++ 函数std::map::end() 返回一个指向映射中末尾之后元素的迭代器。

末尾之后元素是在映射中最后一个元素之后理论上存在的元素。

声明

以下是来自 `std::map` 头文件的 `std::map::end()` 函数声明。

C++98

iterator end();
const_iterator end() const;

C++11

iterator end() noexcept;
const_iterator end() const noexcept;

参数

返回值

如果对象是常量限定的,则方法返回常量迭代器;否则返回非常量迭代器。

异常

此成员函数从不抛出异常。

时间复杂度

常数,即 O(1)

示例

以下示例演示了 `std::map::end()` 函数的用法。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Initializer_list constructor */
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   cout << "Map contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果:

Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
map.htm
广告