命名空间
变体
操作

std::vector<T,Allocator>::end, std::vector<T,Allocator>::cend

来自 cppreference.cn
< cpp‎ | container‎ | vector
 
 
 
 
(1)
iterator end();
(C++11 前)
iterator end() noexcept;
(C++11 起)
(constexpr C++20 起)
(2)
const_iterator end() const;
(C++11 前)
const_iterator end() const noexcept;
(C++11 起)
(constexpr C++20 起)
const_iterator cend() const noexcept;
(3) (C++11 起)
(constexpr C++20 起)

返回指向 vector 中最后一个元素之后元素的迭代器。

此元素充当占位符;尝试访问它会导致未定义行为。

range-begin-end.svg

目录

[编辑] 返回值

指向最后一个元素之后元素的迭代器。

[编辑] 复杂度

常数。

注解

libc++ 向后移植了 cend() 到 C++98 模式。

[编辑] 示例

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
 
int main()
{
    std::vector<int> nums{1, 2, 4, 8, 16};
    std::vector<std::string> fruits{"orange", "apple", "raspberry"};
    std::vector<char> empty;
 
    // Print vector.
    std::for_each(nums.begin(), nums.end(), [](const int n) { std::cout << n << ' '; });
    std::cout << '\n';
 
    // Sums all integers in the vector nums (if any), printing only the result.
    std::cout << "Sum of nums: "
              << std::accumulate(nums.begin(), nums.end(), 0) << '\n';
 
    // Prints the first fruit in the vector fruits, checking if there is any.
    if (!fruits.empty())
        std::cout << "First fruit: " << *fruits.begin() << '\n';
 
    if (empty.begin() == empty.end())
        std::cout << "vector 'empty' is indeed empty.\n";
}

输出

1 2 4 8 16
Sum of nums: 31
First fruit: orange
vector 'empty' is indeed empty.

[编辑] 参见

(C++11 起)
返回指向开始的迭代器
(公开成员函数) [编辑]
(C++11 起)(C++14 起)
返回指向容器或数组末尾的迭代器
(函数模板) [编辑]