- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <span>
The <span> 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 <span> Header
To include the <span> header in your C++ program, you can use the following syntax.
#include <span>
Functions of <span> Header
Below is list of all functions from <span> 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 <iostream> #include <span> 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 <iostream> #include <span> 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