Przejdź do głównej zawartości

std::span begin() method

constexpr iterator begin() const noexcept;

Returns an iterator

to the first element of the view.

If the view is empty, the returned iterator will be equal to end().

Parameters

(none)

Return value

Iterator to the first element.

Complexity

Constant - O(1).

Example

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);
}
Output
1 3 4 5 
1
2 3 4 5
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::span begin() method

constexpr iterator begin() const noexcept;

Returns an iterator

to the first element of the view.

If the view is empty, the returned iterator will be equal to end().

Parameters

(none)

Return value

Iterator to the first element.

Complexity

Constant - O(1).

Example

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);
}
Output
1 3 4 5 
1
2 3 4 5
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.