跳到主要内容

std::vector end() 方法

// prism-push-types:iterator,const_iterator
// Non-const version
constexpr iterator end() noexcept;

// Const version
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;

返回指向数组末尾之后元素的迭代器

指向数组末尾之后元素的迭代器。

如果数组为空,返回的迭代器将等于 begin()

未定义行为

尝试解引用越界迭代器是未定义行为

.

参数

(无)

返回值

指向第一个元素的迭代器。

复杂度

常数 - O(1)

备注

对于容器c,表达式*c.begin()等效于c.front()

end 和 cend 的区别

对于 const 容器 c,end 和 cend 是相同的 - c.end() == c.cend()

对于非常量类型c的容器,它们返回不同的迭代器

#include <vector>

int main()
{
std::vector<int> arr = {1, 2, 3, 4, 5};
auto it = arr.end(); // Type: std::vector<int>::iterator
*std::prev(it) = 5; // ✔ Ok
}

示例

Main.cpp
#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.
本文档源自此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::vector end() 方法

// prism-push-types:iterator,const_iterator
// Non-const version
constexpr iterator end() noexcept;

// Const version
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;

返回指向数组末尾之后元素的迭代器

指向数组末尾之后元素的迭代器。

如果数组为空,返回的迭代器将等于 begin()

未定义行为

尝试解引用越界迭代器是未定义行为

.

参数

(无)

返回值

指向第一个元素的迭代器。

复杂度

常数 - O(1)

备注

对于容器c,表达式*c.begin()等效于c.front()

end 和 cend 的区别

对于 const 容器 c,end 和 cend 是相同的 - c.end() == c.cend()

对于非常量类型c的容器,它们返回不同的迭代器

#include <vector>

int main()
{
std::vector<int> arr = {1, 2, 3, 4, 5};
auto it = arr.end(); // Type: std::vector<int>::iterator
*std::prev(it) = 5; // ✔ Ok
}

示例

Main.cpp
#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.
本文档源自此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。