跳到主要内容

std::span end() 方法

constexpr iterator end() const noexcept;

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

指向视图中最后一个元素之后的位置。

如果视图为空,则返回的迭代器将等于 begin()

参数

(无)

返回值

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

复杂度

常数 - O(1)

示例

Main.cpp
#include <span>
#include <iostream>

void print(std::span<const int> sp)
{
for(auto it = sp.begin(); it != sp.end(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
}

void transmogrify(std::span<int> sp)
{
if (!sp.empty()) {
std::cout << *sp.begin() << '\n';
*sp.begin() = 2;
}
}

int main()
{
int array[] { 1, 3, 4, 5 };
print(array);
transmogrify(array);
print(array);
}
输出
1 3 4 5 
1
2 3 4 5
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::span end() 方法

constexpr iterator end() const noexcept;

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

指向视图中最后一个元素之后的位置。

如果视图为空,则返回的迭代器将等于 begin()

参数

(无)

返回值

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

复杂度

常数 - O(1)

示例

Main.cpp
#include <span>
#include <iostream>

void print(std::span<const int> sp)
{
for(auto it = sp.begin(); it != sp.end(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
}

void transmogrify(std::span<int> sp)
{
if (!sp.empty()) {
std::cout << *sp.begin() << '\n';
*sp.begin() = 2;
}
}

int main()
{
int array[] { 1, 3, 4, 5 };
print(array);
transmogrify(array);
print(array);
}
输出
1 3 4 5 
1
2 3 4 5
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。