函式模板
<iterator> <array> <deque> <forward_list> <list> <map> <regex> <set> <string> <unordered_map> <unordered_set> <vector>
std::end
容器 (1) | template <class Container> auto end (Container& cont) -> decltype (cont.end());template <class Container> auto end (const Container& cont) -> decltype (cont.end()); |
---|
陣列 (2) | template <class T, size_t N> T* end (T(&arr)[N]); |
---|
容器 (1) | template <class Container> auto end (Container& cont) -> decltype (cont.end());template <class Container> auto end (const Container& cont) -> decltype (cont.end()); |
---|
陣列 (2) | template <class T, size_t N> constexpr T* end (T(&arr)[N]) noexcept; |
---|
指向末尾的迭代器
引數
- cont
- 定義了成員 end 的類型別物件。
- arr
- 一個數組。
返回值
對於 (1),與 cont.end() 返回的相同。
對於 (2),指向陣列最後一個元素之後位置的指標。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// std::begin / std::end example
#include <iostream> // std::cout
#include <vector> // std::vector, std::begin, std::end
int main () {
int foo[] = {10,20,30,40,50};
std::vector<int> bar;
// iterate foo: inserting into bar
for (auto it = std::begin(foo); it!=std::end(foo); ++it)
bar.push_back(*it);
// iterate bar: print contents:
std::cout << "bar contains:";
for (auto it = std::begin(bar); it!=std::end(bar); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
輸出
bar contains: 10 20 30 40 50
|
資料競爭
引數被訪問但未被修改。
呼叫不會訪問序列中的任何元素,但返回的迭代器可用於訪問或修改它們。
異常安全
提供與對引數執行的操作相同的保證級別(對於標準容器和陣列,這是無異常保證)。
另見
- begin
- 指向起始位置的迭代器 (函式模板)