GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C++ Library - <span>
Selected Reading
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> HeaderTo include the <span> header in your C++ program, you can use the following syntax. #include <span> Functions of <span> HeaderBelow is list of all functions from <span> header.
Creating a Span from an ArrayIn 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;
}
OutputOutput of the above code is as follows − 2 4 6 8 Slicing a SpanConsider 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;
}
OutputFollowing is the output of the above code − 23 34 45 0 Advertisements
| ||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |