std::unordered_set<Key,Hash,KeyEqual,Allocator>::end, std::unordered_set<Key,Hash,KeyEqual,Allocator>::cend
來自 cppreference.com
iterator end() noexcept; |
(1) | (C++11 起) |
const_iterator end() const noexcept; |
(2) | (C++11 起) |
const_iterator cend() const noexcept; |
(3) | (C++11 起) |
返回指向 unordered_set
中最後一個元素之後一個元素的迭代器。
此元素充當佔位符;嘗試訪問它將導致未定義行為。
目錄 |
[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) |