C++20 introduced the std::span class template as part of the C++ Template Library which represents an object that provides a way to view a contiguous sequence of objects. This feature offers a flexible and efficient way to work with contiguous sequences of objects. It is defined inside <span> header file.
In this article, we will explore the concept of std::span, discuss its usage, and provide examples to showcase its capabilities.
Syntax
std::span <type> span_name;
where type is the type of object in the sequence.
Initialization of std::span
We can initialize std::span using 3 methods:
1. From an array:
std::span<int> span_name(array_name);
2. From a vector:
std::span<int> span_name(vector_name);
3. From a string:
std::span<char> span_name(string_name);
Member functions of std::span
A span has the following member functions used to perform various operations:
- data(): A pointer to the first element of the sequence.
- size(): Returns the size of the sequence that is the number of elements in the sequence.
- empty(): Returns a boolean value that indicates whether the sequence is empty.
- front(): A reference to the first element of the sequence.
- back(): A reference to the last element of the sequence.
- operator[]: An accessor that returns a reference to the element at the specified index.
- at(): An accessor that returns a reference to the element at the specified index, throwing an exception if the index is out of bounds.
- begin(): A function that returns an iterator to the beginning of the sequence.
- end(): A function that returns an iterator to the end of the sequence.
Examples of std::span
Example 1:
The below code demonstrates the usage std::span to create a non-owning view of an array and then prints the elements of the array.
C++
// C++ program to illustrate the use of std::span
#include <iostream>
#include <span>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
// Create a span of int of array
span<int> span_arr(arr);
for (const auto& num : span_arr) {
cout << num << " ";
}
return 0;
}
Output
1 2 3 4 5
Example 2:
The below code demonstrates the usage of std::span to create a non-owning view of a std::vector<int>, and then create a sub-span from the original span.
C++
// C++ program to illustrate the initialization of span
// using vector
#include <iostream>
#include <span>
#include <vector>
int main()
{
vector<int> vec = { 1, 2, 3, 4, 5 };
span<int> span_vec(vec);
// Create a subspan form index 1 to 3
std::span<int> subspan = span_vec.subspan(1, 3);
for (const auto& num : subspan) {
std::cout << num << " ";
}
return 0;
}
Output
2 3 4
Features of std::span
- Non-owning Reference: std::span does not assume ownership of the data it refers to. It acts as a wrapper around the existing data.
- Lightweight: std::span is designed to be efficient, employing a small memory footprint. Typically, it consists of two pointers (begin and end) and a size value.
- Contiguous Sequence: std::span is compatible only with contiguous data structures such as arrays, std::vector, or std::array. It cannot be utilized with non-contiguous data structures like linked lists.
- Safety Measures: std::span incorporates bounds checking, ensuring secure access to the underlying data. It helps prevent common errors such as buffer overflows or underflows.
Conclusion
Spans are a powerful new feature in C++ 20 that can make your code safer, more convenient, and more efficient. If you're not already using spans, I encourage you to give them a try.
Similar Reads
C++ 20 - std::format C++20 comes with a host of new features and improvements, including the format() function. In this article, we will explore how std::format can be used to format strings in C++20. C++20 - std::format std::format is a new function Introduced in C++20 that provides a way to format strings by replacing
4 min read
C++ 23 Standard C++23, officially known as ISO/IEC 14882:2023, is the latest standardized version of the C++ programming language, published in 2023. As C++ introduces new improvements and features every 3 years in the form of a standard like C++20 introduced powerful features such as concepts, ranges, and coroutin
6 min read
std::distance in C++ The std::distance() in C++ STL is a built-in function used to calculate the number of elements between two iterators. It is defined inside <iterator> header file. In this article, we will learn about the std::distance function in C++ with examples.Example:C++// C++ Program to illustrate the us
5 min read
std::plus in c++ It is a Function object for performing addition. The object class whose call returns the result of adding its two arguments (as returned by operator + ). Syntax : template struct plus : binary_function { T operator() (const T& x, const T& y) const { return x + y; } }; Template parameters : T
2 min read
C++ 11 Standard C++ 11, officially known as ISO/IEC 14882:2011, is a significant version of the C++ programming language standard, published in 2011. It marked a major fix up of the language, introducing various features and enhancements that improved the usability, performance, and safety of C++ code. Before C++ 1
5 min read