We may come across cases where we need to write a method but we'd like to have the flexibility to accept a plain array or STL containers as input. std::span solves this problem. It gives the user a view into a contiguous sequence of elements. This recipe will teach you how to use it.
Using span
How to do it...
In this recipe, we'll write a method with one parameter (std::span) that can be used in different contexts. Then, we'll highlight the flexibility it offers:
- Let's start by adding the includes we need. Then, we need to define the print method by passing the container variable of the std::span type:
#include <iostream>
#include <vector>
#include <array>
#include <span>...