C++ Set 庫 - begin 函式



描述

它返回一個迭代器,該迭代器指向 set 容器中的第一個元素。

宣告

以下是 std::set::begin 在不同 C++ 版本中的工作方式。

C++98

iterator begin();
const_iterator begin() const;

C++11

 iterator begin() noexcept;
const_iterator begin() const noexcept;

返回值

它返回一個迭代器,該迭代器指向 set 容器中的第一個元素。

異常

它從不丟擲異常。

時間複雜度

時間複雜度是常數。

示例

以下示例顯示了 std::set::begin 的用法。

#include <iostream>
#include <set>

int main () {
   int myints[] = {50,40,30,20,10};
   std::set<int> myset (myints,myints+10);

   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: 0 1 10 20 30 40 50 26390 2065620553
set.htm
廣告