std::unordered_set<Key,Hash,KeyEqual,Allocator>:: begin, std::unordered_set<Key,Hash,KeyEqual,Allocator>:: cbegin
From cppreference.net
<
cpp
|
container
|
unordered set
|
iterator begin
(
)
noexcept
;
|
(1) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
|
const_iterator begin
(
)
const
noexcept
;
|
(2) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
|
const_iterator cbegin
(
)
const
noexcept
;
|
(3) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
返回指向 * this 首元素的迭代器。
如果 * this 为空,返回的迭代器将等于 end() 。
目录 |
返回值
指向第一个元素的迭代器。
复杂度
常量。
注释
由于
iterator
和
const_iterator
都是常量迭代器(实际上可能是同一类型),因此无法通过这些成员函数返回的迭代器来修改容器中的元素。
示例
运行此代码
#include <iostream> #include <unordered_set> struct Point { double x, y; }; int main() { Point pts[3] = {{1, 0}, {2, 0}, {3, 0}}; // points 是一个包含点地址的集合 std::unordered_set<Point*> points = { pts, pts + 1, pts + 2 }; // 将每个点 (i, 0) 的 y 坐标从 0 改为 i^2 并打印该点 for (auto iter = points.begin(); iter != points.end(); ++iter) { (*iter)->y = ((*iter)->x) * ((*iter)->x); // iter 是指向 Point* 的指针 std::cout << "(" << (*iter)->x << ", " << (*iter)->y << ") "; } std::cout << '\n'; // 现在使用基于范围的 for 循环,将每个 y 坐标增加 10 for (Point* i : points) { i->y += 10; std::cout << "(" << i->x << ", " << i->y << ") "; } }
可能的输出:
(3, 9) (1, 1) (2, 4) (3, 19) (1, 11) (2, 14)
参见
|
返回指向末尾的迭代器
(公开成员函数) |
|
|
(C++11)
(C++14)
|
返回指向容器或数组起始位置的迭代器
(函数模板) |