public member function
<set>

std::set::begin

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

因為 set 容器始終保持其元素有序,begin指向根據容器的 排序準則 排在最前面的元素。

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

引數



返回值

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

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

成員型別iteratorconst_iterator雙向迭代器 型別,指向元素。

示例

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

int main ()
{
  int myints[] = {75,23,65,42,13};
  std::set<int> myset (myints,myints+5);

  std::cout << "myset contains:";
  for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}

輸出
myset contains: 13 23 42 65 75


複雜度

常量。

迭代器有效性

沒有變化。

資料競爭

訪問容器(const 和非 const 版本都不會修改容器)。
併發訪問set的元素是安全的。

異常安全

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

另見