C++ 對映庫 - cend() 函式



描述

C++ 函式std::map::cend() 返回一個指向對映的末尾之後元素的常量迭代器。

此成員函式獲得的迭代器可用於迭代容器,但即使物件本身不是常量,也不能用於修改其指向的物件的內容。

宣告

以下是來自 <map> 標頭檔案的 std::map::cend() 函式的宣告。

C++11

const_iterator cend() const noexcept;

引數

返回值

返回一個常量迭代器。

異常

此成員函式從不丟擲異常。

時間複雜度

常數,即 O(1)

示例

以下示例演示了 std::map::cend() 函式的用法。

#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.cbegin(); it != m.cend(); ++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
廣告