std::span<T,Extent>::begin, std::span<T,Extent>::cbegin
From cppreference.com
| constexpr iterator begin() const noexcept; |
(1) | (C++20以降) |
| constexpr const_iterator cbegin() const noexcept; |
(2) | (C++23から) |
spanの最初の要素へのイテレータを返します。
spanが空の場合、返されるイテレータはend()と等しくなります。
目次 |
[編集] 戻り値
最初の要素へのイテレータ。
[編集] 計算量
定数。
[編集] 例
このコードを実行
#include <iostream> #include <span> void print(std::span<const int> array) { std::cout << "array = "; for (auto it = array.begin(); it != array.end(); ++it) std::cout << *it << ' '; std::cout << '\n'; } void set_first_element(std::span<int> sp, int new_value) { if (!sp.empty()) { std::cout << "old *begin = " << *sp.begin() << '\n'; *sp.begin() = new_value; std::cout << "new *begin = " << *sp.begin() << '\n'; } } int main() { int array[]{1, 3, 4, 5}; print(array); set_first_element(array, 2); print(array); }
出力
array = 1 3 4 5 old *begin = 1 new *begin = 2 array = 2 3 4 5
[編集] 関連項目
| (C++23) |
末尾へのイテレータを返す (public メンバ関数) |
| (C++11)(C++14) |
コンテナまたは配列の先頭を指すイテレータを返す (関数テンプレート) |