名稱空間
變體
操作

std::ranges::range

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

concept range = requires( T& t ) {
    ranges::begin(t); // equality-preserving for forward iterators
    ranges::end (t);

};
(C++20 起)

range 概念定義了一個型別,它透過提供一個迭代器和哨兵來表示範圍內的元素,從而允許迭代其元素。

目錄

[編輯] 語義要求

給定一個表示式 E,使得 decltype((E))TT 只有在以下情況下才符合 range 模型:

[編輯] 注意

一個典型的 range 類只需要提供兩個函式

  1. 一個成員函式 begin(),其返回型別符合 input_or_output_iterator 模型。
  2. 一個成員函式 end(),其返回型別符合 sentinel_for<It> 模型,其中 Itbegin() 的返回型別。

另外,它們也可以是非成員函式,透過引數依賴查詢找到。

[編輯] 示例

#include <ranges>
 
// A minimum range
struct SimpleRange
{
    int* begin();
    int* end();
};
static_assert(std::ranges::range<SimpleRange>);
 
// Not a range: no begin/end
struct NotRange
{
    int t {};
};
static_assert(!std::ranges::range<NotRange>);
 
// Not a range: begin does not return an input_or_output_iterator
struct NotRange2
{
    void* begin();
    int* end();
};
static_assert(!std::ranges::range<NotRange2>);
 
int main() {}

[編輯] 缺陷報告

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

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 3915 C++20 ranges::begin(t)ranges::end(t)
不需要隱式表示式變體
移除了冗餘要求
冗餘描述