std::unordered_set<Key,Hash,KeyEqual,Allocator>::begin, std::unordered_set<Key,Hash,KeyEqual,Allocator>::cbegin
来自 cppreference.cn
< cpp | container | unordered set
iterator begin() noexcept; |
(1) | (since C++11) |
const_iterator begin() const noexcept; |
(2) | (since C++11) |
const_iterator cbegin() const noexcept; |
(3) | (since C++11) |
返回指向 unordered_set
首个元素的迭代器。
如果 unordered_set
为空,则返回的迭代器将等于 end()。
目录 |
[edit] 返回值
指向首个元素的迭代器。
[edit] 复杂度
常数。
注意
由于 iterator
和 const_iterator
都是常量迭代器(并且实际上可能是相同的类型),因此无法通过任何这些成员函数返回的迭代器来修改容器的元素。
[edit] 示例
运行此代码
#include <iostream> #include <unordered_set> struct Point { double x, y; }; int main() { Point pts[3] = {{1, 0}, {2, 0}, {3, 0}}; // points is a set containing the addresses of points std::unordered_set<Point*> points = { pts, pts + 1, pts + 2 }; // Change each y-coordinate of (i, 0) from 0 into i^2 and print the point for (auto iter = points.begin(); iter != points.end(); ++iter) { (*iter)->y = ((*iter)->x) * ((*iter)->x); // iter is a pointer-to-Point* std::cout << "(" << (*iter)->x << ", " << (*iter)->y << ") "; } std::cout << '\n'; // Now using the range-based for loop, we increase each y-coordinate by 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)
[edit] 参见
返回指向末尾的迭代器 (public member function) | |
(C++11)(C++14) |
返回指向容器或数组开头的迭代器 (function template) |