std::set<Key,Compare,Allocator>::begin, std::set<Key,Compare,Allocator>::cbegin

来自cppreference.com
< cpp‎ | container‎ | set
iterator begin();
(C++11 前)
iterator begin() noexcept;
(C++11 起)
const_iterator begin() const;
(C++11 前)
const_iterator begin() const noexcept;
(C++11 起)
const_iterator cbegin() const noexcept;
(C++11 起)

返回指向 set 首元素的迭代器。

set 为空,则返回的迭代器将等于 end()

range-begin-end.svg

目录

[编辑] 参数

(无)

[编辑] 返回值

指向首元素的迭代器。

[编辑] 复杂度

常数。

[编辑] 注意

因为 iteratorconst_iterator 都是常迭代器(而且实际上可以是同一类型),故不可能通过任何这些成员函数返回的迭代器修改容器元素。

[编辑] 示例

#include <algorithm>
#include <iostream>
#include <set>
 
int main() {
  std::set<int> set = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
  std::for_each(set.cbegin(), set.cend(), [](int x) {
    std::cout << x << ' ';
  });
  std::cout << '\n';
}

输出:

1 2 3 4 5 6 9

[编辑] 参阅

(C++11)
返回指向末尾的迭代器
(公开成员函数) [编辑]