std::span begin() 方法
- 自 C++20 起
constexpr iterator begin() const noexcept;
返回指向数组末尾之后元素的迭代器。
指向视图的第一个元素。如果视图为空,返回的迭代器将等于end()
。
参数
(无)
返回值
指向第一个元素的迭代器。
复杂度
常数 - 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