首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::end

Defined in header <iterator>

(1)

template< class C > auto end( C& c ) -> decltype(c.end());

(since C++11) (until C++17)

template< class C > constexpr auto end( C& c ) -> decltype(c.end());

(since C++17)

(1)

template< class C > auto end( const C& c ) -> decltype(c.end());

(since C++11) (until C++17)

template< class C > constexpr auto end( const C& c ) -> decltype(c.end());

(since C++17)

(2)

template< class T, std::size_t N > T* end( T (&array)N );

(since C++11) (until C++14)

template< class T, std::size_t N > constexpr T* end( T (&array)N ) noexcept;

(since C++14)

template< class C > constexpr auto cend( const C& c ) noexcept(/* see below */) -> decltype(std::end(c));

(3)

(since C++14)

将迭代器返回到%28i.e。给定容器的最后一个元素%29之后的元素c或阵列array这些模板依赖于C::end()有合理的实施。

1%29准确返回c.end(),它通常是迭代器,它位于c.如果C是一个标准Container,则返回C::iterator何时c是不合格的C::const_iterator否则。

2%29返回指向数组末尾的指针。array...

3%29准确返回std::end(c),与c总是被认为是合格的。如果C是一个标准Container,这总是返回一个C::const_iterator...

参数

c

-

a container with an end method

array

-

an array of arbitrary type

返回值

的末尾的迭代器。carray请注意,容器或数组的末尾定义为最后一个有效元素之后的元素。

例外

3%29

noexcept规格:

noexcept(noexcept(std::end(c)))

注记

除了被纳入<iterator>,,,std::endstd::cend如果包括下列任何一个标头,则保证可用:<array>,,,<deque>,,,<forward_list>,,,<list>,,,<map>,,,<regex>,,,<set>,,,<string>,<字符串[医]自C++17%29以来,视图>%28,<unordered_map>,,,<unordered_set>,和<vector>...

用户定义的重载

自定义超载end可以为不公开合适的类提供end()成员函数,但可以迭代。标准库已经提供了以下重载:

std::end(std::initializer_list) (C++11)

specializes std::end (function template)

std::end(std::valarray) (C++11)

specializes std::end (function template)

begin(std::filesystem::directory_iterator)end(std::filesystem::directory_iterator)

range-based for loop support (function)

begin(std::filesystem::recursive_directory_iterator)end(std::filesystem::recursive_directory_iterator)

range-based for loop support (function)

类似于使用swap28%Swappable%29,典型使用end函数在泛型上下文中相当于using std::end; end(arg);,这使得ADL-为用户定义的类型和标准库函数模板选择的重载将出现在相同的重载集中。

二次

代码语言:javascript
复制
template<typename Container, typename Function>
void for_each(Container&& cont, Function f) {
    using std::begin;
    auto it = begin(cont);
    using std::end;
    auto end_it = end(cont);
    while (it != end_it) {
        f(*it);
        ++it;
    }
}

二次

二次

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
 
int main() 
{
    std::vector<int> v = { 3, 1, 4 };
    if (std::find(std::begin(v), std::end(v), 5) != std::end(v)) {
        std::cout << "found a 5 in vector v!\n";
    }
 
    int a[] = { 5, 10, 15 };
    if (std::find(std::begin(a), std::end(a), 5) != std::end(a)) {
        std::cout << "found a 5 in array a!\n";
    }
}

二次

产出:

二次

代码语言:javascript
复制
found a 5 in array a!

二次

另见

begincbegin (C++11)(C++14)

returns an iterator to the beginning of a container or array (function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券