名稱空間
變體
操作

std::ranges::iota, std::ranges::iota_result

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
摺疊操作
數值操作
iota
(C++23)            
對未初始化儲存的操作
返回型別
 
 
定義於標頭檔案 <numeric>
呼叫簽名 (Call signature)
template< std::input_or_output_iterator O, std::sentinel_for<O> S,

          std::weakly_incrementable T >
requires std::indirectly_writable<O, const T&>
constexpr iota_result<O, T>

    iota( O first, S last, T value );
(1) (C++23 起)
template< std::weakly_incrementable T, ranges::output_range<const T&> R >

constexpr iota_result<ranges::borrowed_iterator_t<R>, T>

    iota( R&& r, T value );
(2) (C++23 起)
輔助型別
template< class O, class T >
using iota_result = ranges::out_value_result<O, T>;
(3) (C++23 起)

用從 value 開始並重復計算 ++value 的連續遞增值填充範圍 [firstlast)

等價操作

*(first)     = value;
*(first + 1) = ++value;
*(first + 2) = ++value;
*(first + 3) = ++value;
...

目錄

[編輯] 引數

first, last - 定義要用從 value 開始的順序遞增值填充的元素 範圍 的迭代器-哨兵對
value - 要儲存的初始值;表示式 ++value 必須是合法的

[編輯] 返回值

{last, value + ranges::distance(first, last)}

[編輯] 複雜度

恰好 last - first 次增量和賦值。

[編輯] 可能實現

struct iota_fn
{
    template<std::input_or_output_iterator O, std::sentinel_for<O> S,
            std::weakly_incrementable T>
    requires std::indirectly_writable<O, const T&>
    constexpr iota_result<O, T> operator()(O first, S last, T value) const
    {
        while (first != last)
        {
            *first = as_const(value);
            ++first;
            ++value;
        }
        return {std::move(first), std::move(value)};
    }
 
    template<std::weakly_incrementable T, std::ranges::output_range<const T&> R>
    constexpr iota_result<std::ranges::borrowed_iterator_t<R>, T>
    operator()(R&& r, T value) const
    {
        return (*this)(std::ranges::begin(r), std::ranges::end(r), std::move(value));
    }
};
 
inline constexpr iota_fn iota;

[編輯] 注意

此函式以程式語言 APL 中的整數函式 命名。

特性測試 標準 特性
__cpp_lib_ranges_iota 202202L (C++23) std::ranges::iota

[編輯] 示例

使用迭代器 vector (std::vector<std::list<T>::iterator>) 作為代理來打亂 std::list 的元素,因為 ranges::shuffle 無法直接應用於 std::list

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>
 
template <typename Proj = std::identity>
void println(auto comment, std::ranges::input_range auto&& range, Proj proj = {})
{
    for (std::cout << comment; auto const &element : range)
        std::cout << proj(element) << ' ';
    std::cout << '\n';
}
 
int main()
{
    std::list<int> list(8);
 
    // Fill the list with ascending values: 0, 1, 2, ..., 7
    std::ranges::iota(list, 0);
    println("List: ", list);
 
    // A vector of iterators (see the comment to Example)
    std::vector<std::list<int>::iterator> vec(list.size());
 
    // Fill with iterators to consecutive list's elements
    std::ranges::iota(vec.begin(), vec.end(), list.begin());
 
    std::ranges::shuffle(vec, std::mt19937 {std::random_device {}()});
    println("List viewed via vector: ", vec, [](auto it) { return *it; });
}

可能的輸出

List: 0 1 2 3 4 5 6 7
List viewed via vector: 5 7 6 0 1 3 4 2

[編輯] 參閱

將給定值複製賦給一個範圍中的每個元素
(函式模板) [編輯]
給一個範圍的元素賦某個值
(演算法函式物件)[編輯]
將連續函式呼叫的結果賦給一個範圍中的每個元素
(函式模板) [編輯]
將一個函式的結果儲存在一個範圍中
(演算法函式物件)[編輯]
由重複遞增初始值生成的序列組成的 view
(類模板) (定製點物件)[編輯]
(C++11)
以起始值的連續增量填充一個範圍
(函式模板) [編輯]