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
广告

© . All rights reserved.