當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ std::iota用法及代碼示例


存儲順序遞增
分配val的[first,last]個連續值範圍內的每個元素,就像在寫入每個元素之後以++ val遞增。

模板:

void iota (ForwardIterator first, ForwardIterator last, T val);

參數:

first, last
Forward iterators to the initial and final positions of the sequence
to be written. The range used is [first, last), which contains all the
elements between first and last, including the element pointed by
first but not the element pointed by last.

val
Initial value for the accumulator. 

返回類型:
None
// CPP program to illustrate 
// std::iota 
#include <iostream> // std::cout 
#include <numeric> // std::iota 
  
// Driver code 
int main() 
{ 
    int numbers[10]; 
    // Initailising starting value as 100 
    int st = 100; 
  
    std::iota(numbers, numbers + 10, st); 
  
    std::cout << "Elements are:"; 
    for (auto i:numbers) 
        std::cout << ' ' << i; 
    std::cout << '\n'; 
  
    return 0; 
}

輸出:


Elements are:100 101 102 103 104 105 106 107 108 109

應用:
它可用於生成連續的數字序列。

// CPP program to generate 
// a sequence of numbers using std::iota 
#include <iostream> // std::cout 
#include <numeric> // std::iota 
  
// Driver code 
int main() 
{ 
    int numbers[11]; 
    // Initailising starting value as 10 
    int st = 10; 
  
    std::iota(numbers, numbers + 11, st); 
  
    std::cout << "Elements are:"; 
    for (auto i:numbers) 
        std::cout << ' ' << i; 
    std::cout << '\n'; 
  
    return 0; 
}

輸出:

Elements are:10 11 12 13 14 15 16 17 18 19 20


相關用法


注:本文由純淨天空篩選整理自 std::iota in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。