VOOZH about

URL: https://www.geeksforgeeks.org/cpp/cpp-20-std-span/

⇱ C++ 20 - std::span - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C++ 20 - std::span

Last Updated : 6 Jun, 2023

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.


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.


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.

Comment
Article Tags:
Article Tags: