名稱空間
變體
操作

std::ranges::contiguous_range

來自 cppreference.com
< cpp‎ | ranges
 
 
範圍庫 (Ranges library)
範圍介面卡 (Range adaptors)
 
定義於標頭檔案 <ranges>
template< class T >

concept contiguous_range =
    ranges::random_access_range<T> &&
    std::contiguous_iterator<ranges::iterator_t<T>> &&
    requires(T& t) {
        { ranges::data(t) } ->
            std::same_as<std::add_pointer_t<ranges::range_reference_t<T>>>;

    };
(C++20 起)

contiguous_range 概念是 range 的一個細化,其 ranges::begin 返回 contiguous_iterator 的模型,且定製點 ranges::data 可用。

[編輯] 語義要求

T 建模 contiguous_range 僅當給定表示式 e 使得 decltype((e))T& 時,std::to_address(ranges::begin(e)) == ranges::data(e)

[編輯] 示例

#include <array>
#include <deque>
#include <list>
#include <mdspan>
#include <ranges>
#include <set>
#include <span>
#include <string_view>
#include <valarray>
#include <vector>
 
template<typename T>
concept CR = std::ranges::contiguous_range<T>;
 
// zstring being a ranges::contiguous_range doesn't have to be a ranges::sized_range
struct zstring
{
    struct sentinel
    {
        friend constexpr bool operator==(const char* str, sentinel) noexcept
        { return *str == '\0'; }
    };
 
    const char* str;
 
    const char* begin() const noexcept { return str; }
    sentinel end() const noexcept { return {}; }
};
 
int main()
{
    int a[4];
    static_assert(
            CR<std::vector<int>> and
        not CR<std::vector<bool>> and
        not CR<std::deque<int>> and
            CR<std::valarray<int>> and
            CR<decltype(a)> and
        not CR<std::list<int>> and
        not CR<std::set<int>> and
            CR<std::array<std::list<int>,42>> and
            CR<std::string_view> and
            CR<zstring> and
            CR<std::span<const int>> and
        not CR<std::mdspan<int, std::dims<1>>>
    );
}

[編輯] 參閱

指定範圍在常數時間內知道其大小
(概念) [編輯]
指定其迭代器型別滿足 random_access_iterator 的範圍
(概念) [編輯]