跳到主要内容

std::span front() 方法

constexpr reference front() const;

返回对指定索引pos处元素的引用

指向容器中的第一个元素。

未定义行为

在空 span 上调用 front()未定义行为

.

参数

(无)

返回值

对第一个元素的引用。

复杂度

常数 - O(1)

异常

(无)

备注

对于一个 span s,表达式 s.front() 等价于 *s.begin()s[0]

示例

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

void print(std::span<const int> const data)
{
for (auto offset{0U}; offset != data.size(); ++offset) {
std::cout << data.subspan(offset).front() << ' ';
}
std::cout << '\n';
}

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

std::span front() 方法

constexpr reference front() const;

返回对指定索引pos处元素的引用

指向容器中的第一个元素。

未定义行为

在空 span 上调用 front()未定义行为

.

参数

(无)

返回值

对第一个元素的引用。

复杂度

常数 - O(1)

异常

(无)

备注

对于一个 span s,表达式 s.front() 等价于 *s.begin()s[0]

示例

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

void print(std::span<const int> const data)
{
for (auto offset{0U}; offset != data.size(); ++offset) {
std::cout << data.subspan(offset).front() << ' ';
}
std::cout << '\n';
}

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