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
廣告