Namespaces
Variants

std:: iota

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더에 정의됨 <numeric>
template < class ForwardIt, class T >
void iota ( ForwardIt first, ForwardIt last, T value ) ;
(C++11부터)
(C++20부터 constexpr)

범위 [ first , last ) 를 순차적으로 증가하는 값으로 채웁니다. 시작 값은 value 이며, 반복적으로 ++ value 를 평가합니다.

동등한 연산 (가정: ++ value 가 증가된 값을 반환한다고 가정):

*first   = value;
*++first = ++value;
*++first = ++value;
*++first = ++value;
// "last"에 도달할 때까지 반복

다음 조건 중 하나라도 충족되면 프로그램의 형식이 잘못되었습니다:

  • T ForwardIt 값 타입 으로 변환 가능하지 않습니다.
  • 표현식 ++ val 이 잘못된 형식입니다. 여기서 val T 타입의 변수입니다.

목차

매개변수

first, last - 순차적으로 증가하는 값으로 채울 요소들의 범위 를 정의하는 반복자 쌍 (시작 값은 value )
value - 저장할 초기값

복잡도

정확히 std:: distance ( first, last ) 회의 증분과 할당이 수행됩니다.

가능한 구현

template<class ForwardIt, class T>
constexpr // C++20부터
void iota(ForwardIt first, ForwardIt last, T value)
{
    for (; first != last; ++first, ++value)
        *first = value;
}

참고 사항

이 함수는 프로그래밍 언어 APL 의 정수 함수 에서 이름을 따왔습니다. 이는 C++98에는 포함되지 않았지만 C++11에서 표준 라이브러리에 포함된 STL 구성 요소 중 하나였습니다.

예제

다음 예제는 std::shuffle vector std::list 반복자들에 적용합니다. std::iota 는 컨테이너들을 채우는 데 사용됩니다.

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>
class BigData // inefficient to copy
{
    int data[1024]; /* some raw data */
public:
    explicit BigData(int i = 0) { data[0] = i; /* ... */ }
    operator int() const { return data[0]; }
    BigData& operator=(int i) { data[0] = i; return *this; }
    /* ... */
};
int main()
{
    std::list<BigData> l(10);
    std::iota(l.begin(), l.end(), -4);
    std::vector<std::list<BigData>::iterator> v(l.size());
    std::iota(v.begin(), v.end(), l.begin());
    // Vector of iterators (to original data) is used to avoid expensive copying,
    // and because std::shuffle (below) cannot be applied to a std::list directly.
    std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
    std::cout << "Original contents of the list l:\t";
    for (const auto& n : l)
        std::cout << std::setw(2) << n << ' ';
    std::cout << '\n';
    std::cout << "Contents of l, viewed via shuffled v:\t";
    for (const auto i : v)
        std::cout << std::setw(2) << *i << ' ';
    std::cout << '\n';
}

가능한 출력:

Original contents of the list l:	-4 -3 -2 -1  0  1  2  3  4  5
Contents of l, viewed via shuffled v:	-1  5 -4  0  2  1  4 -2  3 -3

참고 항목

시작 값의 연속적인 증가로 범위를 채웁니다
(알고리즘 함수 객체)
주어진 값을 범위의 모든 요소에 복사 할당합니다
(함수 템플릿)
요소 범위에 특정 값을 할당합니다
(알고리즘 함수 객체)
연속적인 함수 호출의 결과를 범위의 모든 요소에 할당합니다
(함수 템플릿)
함수의 결과를 범위에 저장합니다
(알고리즘 함수 객체)
초기 값을 반복적으로 증가시켜 생성된 시퀀스로 구성된 view
(클래스 템플릿) (커스터마이제이션 포인트 객체)
적응된 시퀀스의 각 요소를 요소의 위치와 값 모두를 포함하는 튜플로 매핑하는 view
(클래스 템플릿) (범위 어댑터 객체)