public member function
<map>

std::map::begin

      iterator begin();const_iterator begin() const;
      iterator begin() noexcept;const_iterator begin() const noexcept;
返回指向開頭的迭代器
返回一個指向 map 容器中第一個元素的迭代器。

因為 map 容器始終保持其元素有序,begin指向的元素根據容器的 排序準則 排在首位。

如果容器 為空,則不應對返回的迭代器進行解引用。

引數



返回值

指向容器中第一個元素的迭代器。

如果 map 物件是 const 限定的,則該函式返回一個const_iterator。否則,它返回一個iterator.

成員型別iteratorconst_iterator是指向元素的(型別為value_type).
請注意,value_type在 map 容器中是以下型別的別名pair<const key_type, mapped_type>.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// map::begin/end
#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  // show content:
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

  return 0;
}

輸出
a => 200
b => 100
c => 300


複雜度

常量。

迭代器有效性

沒有變化。

資料競爭

訪問容器(const 和非 const 版本都不會修改容器)。
呼叫該函式不訪問任何包含的元素,但返回的迭代器可用於訪問或修改元素。併發訪問或修改不同元素是安全的。

異常安全

無異常保證:此成員函式從不丟擲異常。
還可以保證返回的迭代器的複製構造或賦值永遠不會引發異常。

另見