
- C++ Library - Home
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ STL Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ Advanced Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library -
The header in C++20, introduces the std::span class template, which provides a way to represent contiguous sequences of data, such as arrays or vectors, without copying or moving the data.
Unlike traditional containers (like std::vector or std::array), a span does not own the data it references. Instead, it offers a view, means it acts as a lightweight wrapper around existing data, making it ideal to use in the scenarios where we need to pass arrays without incurring the overhead of copying.
Including Header
To include the header in your C++ program, you can use the following syntax.
#include
Functions of Header
Below is list of all functions from header.
Sr.No | Functions & Description |
---|---|
1 |
operator=
It assigns a span. |
2 |
begin
It returns an iterator to the beginning. |
3 |
cbegin
It returns an constant iterator to the beginning. |
4 |
end
It returns an iterator to the end. |
5 |
cend
It returns an constant iterator to the end. |
6 |
rbegin
It returns a reverse iterator to the beginning. |
7 |
crbegin
It returns a constant reverse iterator to the beginning. |
8 |
rend
It returns a reverse iterator to the end. |
9 |
crend
It returns a constant reverse iterator to the end. |
10 |
front
It access the first element. |
11 |
back
It access the last element. |
12 |
at
It access specified element with bounds checking. |
13 |
operator[]
It access specified element. |
14 |
data
It direct access to the underlying contiguous storage. |
15 |
size
It returns the number of elements. |
16 |
size_bytes
It returns the size of the sequence in bytes. |
17 |
empty
It checks if the sequence is empty. |
18 |
first
It obtains a subspan consisting of the first N elements of the sequence. |
19 |
last
It obtains a subspan consisting of the last N elements of the sequence. |
20 |
subspan
It obtains a subspan. |
Creating a Span from an Array
In the following example, we are going to create a span from a allocated array.
#include#include void a(std::span < int > x) { for (int b: x) { std::cout << b << " "; } std::cout << std::endl; } int main() { int array[] = {2,4,6,8}; std::span < int > x(array); a(x); return 0; }
Output
Output of the above code is as follows −
2 4 6 8
Slicing a Span
Consider the following example, where are going to slice a span to create a view of a subset of its elements.
#include#include void a(std::span < int > x) { for (int b: x) { std::cout << b << " "; } std::cout << std::endl; } int main() { int array[] = {1,12,23,34,45}; std::span < int > x(array); auto slice = x.subspan(2, 4); a(slice); return 0; }
Output
Following is the output of the above code −
23 34 45 0