std::span front() 方法
- 自 C++20 起
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