名稱空間
變體
操作

std::span

來自 cppreference.com
< cpp‎ | 容器
在標頭檔案 <span> 中定義
模板<

    T,
    std::size_t Extent = std::dynamic_extent

> span;
(C++20 起)

類模板 span 描述了一個物件,該物件可以引用一個連續的物件序列,序列的第一個元素位於位置零。span 可以具有靜態範圍,在這種情況下,序列中的元素數量在編譯時已知並編碼在型別中,或者具有動態範圍

對於一個 span s,當某個操作使範圍 [s.data()s.data() + s.size()) 內的指標失效時,指向 s 元素的指標、迭代器和引用也會失效。

std::span 的每個特化都是一個 TriviallyCopyable 型別。

(C++23 起)

典型的實現持有指向 T 的指標,如果範圍是動態的,實現還會持有一個大小。

目錄

[編輯] 模板引數

T - 元素型別;必須是完整的物件型別,且不是抽象類型別
Extent - 序列中的元素數量,如果是動態的,則為 std::dynamic_extent

[編輯] 成員型別

成員型別 定義
element_type T
value_type std::remove_cv_t<T>
size_type std::size_t
difference_type std::ptrdiff_t
pointer T*
const_pointer const T*
reference T&
const_reference const T&
iterator 實現定義的 LegacyRandomAccessIteratorConstexprIteratorcontiguous_iterator,其 value_typevalue_type
const_iterator (C++23 起) std::const_iterator<iterator>
reverse_iterator std::reverse_iterator<iterator>
const_reverse_iterator (C++23 起) std::const_iterator<reverse_iterator>

注意:如果 T 沒有 const 限定,則 iterator 是可變迭代器。

Container 的迭代器型別的所有要求也適用於 spaniterator 型別。

[編輯] 成員常量

名稱
constexpr std::size_t extent
[靜態]
Extent
(public static 成員常量)

[編輯] 成員函式

構造一個 span
(公共成員函式) [編輯]
賦值一個 span
(公共成員函式) [編輯]
(解構函式)
(隱式宣告)
銷燬一個 span
(公開成員函式)
迭代器
(C++23 起)
返回指向起始的迭代器
(公共成員函式) [編輯]
(C++23 起)
返回指向末尾的迭代器
(公共成員函式) [編輯]
(C++23 起)
返回指向起始的逆向迭代器
(公共成員函式) [編輯]
(C++23 起)
返回指向末尾的逆向迭代器
(公共成員函式) [編輯]
元素訪問
訪問第一個元素
(公共成員函式) [編輯]
訪問最後一個元素
(公共成員函式) [編輯]
(C++26)
訪問指定的元素,帶邊界檢查
(公共成員函式) [編輯]
訪問指定的元素
(公共成員函式) [編輯]
直接訪問底層連續儲存
(公共成員函式) [編輯]
觀察器
返回元素數量
(公共成員函式) [編輯]
返回序列的位元組大小
(公共成員函式) [編輯]
檢查序列是否為空
(公共成員函式) [編輯]
子檢視
獲取由序列前 N 個元素組成的子 span
(公共成員函式) [編輯]
獲取由序列後 N 個元素組成的子 span
(公共成員函式) [編輯]
獲取一個子 span
(公共成員函式) [編輯]

[編輯] 非成員函式

將 `span` 轉換為其底層位元組的檢視
(函式模板) [編輯]

[編輯] 非成員常量

型別為 std::size_t 的常量,表示 span 具有動態範圍
(常量) [編輯]

[編輯] 輔助模板

模板< T, std::size_t Extent >
constexpr bool ranges::enable_borrowed_range<std::span<T, Extent>> = true;
(C++20 起)

ranges::enable_borrowed_range 的特化使得 span 滿足 borrowed_range

模板< T, std::size_t Extent >
constexpr bool ranges::enable_view<std::span<T, Extent>> = true;
(C++20 起)

ranges::enable_view 的特化使得 span 滿足 view

[編輯] 推導指南

[編輯] 備註

std::span 的特化在所有現有實現中已經都是平凡可複製型別,甚至在 C++23 引入正式要求之前。

特性測試 標準 特性
__cpp_lib_span 202002L (C++20) std::span
202311L (C++26) std::span::at
__cpp_lib_span_initializer_list 202311L (C++26) std::initializer_list 構造 std::span

[編輯] 示例

此示例使用 std::span 在連續範圍內實現一些演算法。

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <span>
 
template<class T, std::size_t N>
[[nodiscard]]
constexpr auto slide(std::span<T, N> s, std::size_t offset, std::size_t width)
{
    return s.subspan(offset, offset + width <= s.size() ? width : 0U);
}
 
template<class T, std::size_t N, std::size_t M>
constexpr bool starts_with(std::span<T, N> data, std::span<T, M> prefix)
{
    return data.size() >= prefix.size()
        && std::equal(prefix.begin(), prefix.end(), data.begin());
}
 
template<class T, std::size_t N, std::size_t M>
constexpr bool ends_with(std::span<T, N> data, std::span<T, M> suffix)
{
    return data.size() >= suffix.size()
        && std::equal(data.end() - suffix.size(), data.end(),
                      suffix.end() - suffix.size());
}
 
template<class T, std::size_t N, std::size_t M>
constexpr bool contains(std::span<T, N> span, std::span<T, M> sub)
{
    return std::ranges::search(span, sub).begin() != span.end();
}
 
void println(const auto& seq)
{
    for (const auto& elem : seq)
        std::cout << elem << ' ';
    std::cout << '\n';
}
 
int main()
{
    constexpr int a[]{0, 1, 2, 3, 4, 5, 6, 7, 8};
    constexpr int b[]{8, 7, 6};
    constexpr static std::size_t width{6};
 
    for (std::size_t offset{}; ; ++offset)
        if (auto s = slide(std::span{a}, offset, width); !s.empty())
            println(s);
        else
            break;
 
    static_assert(""
        && starts_with(std::span{a}, std::span{a, 4})
        && starts_with(std::span{a + 1, 4}, std::span{a + 1, 3})
        && !starts_with(std::span{a}, std::span{b})
        && !starts_with(std::span{a, 8}, std::span{a + 1, 3})
        && ends_with(std::span{a}, std::span{a + 6, 3})
        && !ends_with(std::span{a}, std::span{a + 6, 2})
        && contains(std::span{a}, std::span{a + 1, 4})
        && !contains(std::span{a, 8}, std::span{a, 9})
    );
}

輸出

0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 3203 C++20 不清楚何時 span 元素的指標、迭代器和
引用會失效
已明確
LWG 3903 C++20 span 的解構函式宣告不必要 移除了宣告
P2325R3 C++20 非零靜態範圍的 span 不是一個 view 任何 span 都是一個 view

[編輯] 另請參見

(C++23)
一個多維非擁有陣列檢視
(類模板) [編輯]
將迭代器-哨兵對組合成一個 view
(類模板) [編輯]
引用在列表初始化中建立的臨時陣列
(類模板) [編輯]
只讀字串檢視
(類模板) [編輯]