skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
hpp-span
This page is read only. You can view the source, but not change it.
# <span> **[<span>](https://en.cppreference.com/w/cpp/header/span)** is a non-owning view over a contiguous range of elements (C++20): it holds a pointer and length, allowing you to pass array data without copying. Use it as a function parameter to accept any contiguous container. It's like a safer `const T*` / `size_t` pair. This example passes both vector and C-array data to the same function via span, demonstrating generic array viewing. ```cpp // compile: g++ -std=c++20 -o spanexample spanexample.cpp // run: ./spanexample // description: span as a generic array view #include <span> #include <vector> #include <iostream> void print_span(std::span<const int> s) { for (int x : s) std::cout << x << " "; std::cout << "\n"; } int main() { std::vector<int> v = {1, 2, 3, 4, 5}; print_span(v); int arr[] = {10, 20, 30}; print_span(arr); return 0; } ```
Page Tools
Show pagesource
Backlinks
Back to top