Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::span::subspan(3)	      C++ Standard Libary	 std::span::subspan(3)

NAME
       std::span::subspan - std::span::subspan

Synopsis
	  template< std::size_t	Offset,

		    std::size_t	 Count	=  std::dynamic_extent	>	   (1)
       (since C++20)
	  constexpr std::span<element_type, E /* see below */>

	      subspan()	const;
	  constexpr std::span<element_type, std::dynamic_extent>

	      subspan(	size_type   Offset,				   (2)
       (since C++20)

		       size_type Count = std::dynamic_extent ) const;

	  Obtains  a  span that	is a view over the Count elements of this span
       starting	at
	  offset Offset. If Count is std::dynamic_extent, the number  of  ele-
       ments in	the
	  subspan is size() - offset (i.e., it ends at the end of *this).

	  1) Is	ill-formed if
	    * Offset is	greater	than Extent, or
	    *  Count  is not std::dynamic_extent and Count is greater than Ex-
       tent - Offset.

	  The behavior is undefined if either Offset or	Count is out of	range.
       This happens if

	    * Offset is	greater	than size(), or
	    * Count is not  std::dynamic_extent	 and  Count  is	 greater  than
       size() -	Offset.

	  The extent E of the span returned by (1) is determined as follows:

	    * If Count is not std::dynamic_extent, Count;
	    *  Otherwise,  if Extent is	not std::dynamic_extent, Extent	- Off-
       set;
	    * Otherwise, std::dynamic_extent.

Return value
	  The requested	subspan	r, such	that r.data() == this->data()  +  Off-
       set. If Count is
	  std::dynamic_extent,	r.size()  ==  this->size() - Offset; otherwise
       r.size()	== Count.

Example
       // Run this code

	#include <algorithm>
	#include <cstdio>
	#include <numeric>
	#include <ranges>
	#include <span>

	void display(std::span<const char> abc)
	{
	    const auto columns{20U};
	    const auto rows{abc.size() - columns + 1};

	    for	(auto offset{0U}; offset < rows; ++offset)
	    {
		std::ranges::for_each(abc.subspan(offset,	     columns),
       std::putchar);
		std::putchar('\n');
	    }
	}

	int main()
	{
	    char abc[26];
	    std::iota(std::begin(abc), std::end(abc), 'A');
	    display(abc);
	}

Output:
	ABCDEFGHIJKLMNOPQRST
	BCDEFGHIJKLMNOPQRSTU
	CDEFGHIJKLMNOPQRSTUV
	DEFGHIJKLMNOPQRSTUVW
	EFGHIJKLMNOPQRSTUVWX
	FGHIJKLMNOPQRSTUVWXY
	GHIJKLMNOPQRSTUVWXYZ

See also
	  first	 obtains  a  subspan consisting	of the first N elements	of the
       sequence
		(public	member function)
	  last	obtains	a subspan consisting of	the last N elements of the se-
       quence
		(public	member function)

http://cppreference.com		  2024.06.10		 std::span::subspan(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::span::subspan&sektion=3&manpath=FreeBSD+Ports+15.1.quarterly>

home | help