當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Map end()用法及代碼示例


描述

C++ 函數std::map::end()返回一個指向的迭代器past-the-endMap中的元素。

這個past-the-endelement 是跟在Map中最後一個元素之後的理論元素。

聲明

以下是 std::map::end() 函數形式 std::map 頭的聲明。

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

相關用法


注:本文由純淨天空篩選整理自 C++ Map Library - end() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。