algorithm_iota

algorithm_iota (연속 증가 값 채우기)

std::iota는 범위 [first, last)value에서 시작해 1씩 증가하는 값으로 채워요. APL의 그리스 문자 ι(이오타)에서 유래했어요.

출처: cppreference

본문

std::iota는 범위 [first, last)value로 시작해서 ++value를 반복적으로 평가한 값들로 채워요. <numeric> 헤더에 정의되어 있어요.

template< class ForwardIt, class T >
void iota( ForwardIt first, ForwardIt last, T value );

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

T value = /* initial value */;
while (first != last)
    *first++ = value++;

반환값 (Return value)

없음 (void).

복잡도 (Complexity)

정확히 std::distance(first, last)번의 증가와 배정이 필요해요.

예제 (Example)

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>

int main()
{
    std::list<int> l(10);
    std::iota(l.begin(), l.end(), -4);

    std::vector<std::list<int>::iterator> v(l.size());
    std::iota(v.begin(), v.end(), l.begin());

    for (auto it : v) std::cout << *it << ' ';
    std::cout << '\n';
}

출력:

-4 -3 -2 -1 0 1 2 3 4 5

더 알아보기 (Learn more)

cppreference