跳到主要内容

std::set end() 方法

// Non const version
iterator end() noexcept;

// Const version
iterator end() const noexcept;

// Const version
const_iterator cend() const noexcept;

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

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

尝试解引用末尾之后的迭代器是未定义行为

.

参数

(无)

返回值

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

异常

(无)

复杂度

常数 - O(1)

end 和 cend 的区别

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

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

#include <set>

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

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <set>

int main() {
std::set<int> set = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
std::for_each(set.cbegin(), set.cend(), [](int x) {
std::cout << x << ' ';
});
std::cout << '\n';
}
输出
1 2 3 4 5 6 9
本文档源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::set end() 方法

// Non const version
iterator end() noexcept;

// Const version
iterator end() const noexcept;

// Const version
const_iterator cend() const noexcept;

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

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

尝试解引用末尾之后的迭代器是未定义行为

.

参数

(无)

返回值

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

异常

(无)

复杂度

常数 - O(1)

end 和 cend 的区别

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

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

#include <set>

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

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <set>

int main() {
std::set<int> set = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
std::for_each(set.cbegin(), set.cend(), [](int x) {
std::cout << x << ' ';
});
std::cout << '\n';
}
输出
1 2 3 4 5 6 9
本文档源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。