std::iota
cppreference.com
<tbody>
</tbody>
| <numeric> 에 정의되어 있음.
|
||
template< class ForwardIterator, class T > void iota( ForwardIterator first, ForwardIterator last, T value ); |
(since C++11) | |
value값에서 시작하여++value를 반복하여 얻어지는 값으로 주어진 범위[first, last)를 채운다.
동일 작업:
*(d_first) = value;
*(d_first+1) = ++value;
*(d_first+2) = ++value;
*(d_first+3) = ++value;
...
인자
| first, last | - | 시작값부터 순차적으로 증가하면서 채울 요소의 범위 |
| value | - | 채워질 첫번째 값. ++value가 반드시 동작해야 함. |
반환값
(없음)
Complexity
정확히 last - first 만큼의 증가 및 할당 연산.
가능한 구현
template<class ForwardIterator, class T>
void iota(ForwardIterator first, ForwardIterator last, T value)
{
while(first != last) {
*first++ = value;
++value;
}
}
|
노트
이 함수의 이름은 APL 프로그래밍 언어의 정수 함수 ⍳ 이후에 지어졌다. C++98에 포함되지 않은 STL 구성요소중 하나였지만, C++11에서 표준 라이브러리에 포함되었다.
예제
The following example applies std::shuffle to a vector of std::list iterators since std::shuffle cannot be applied to a std::list directly. std::iota is used to populate both containers.
코드 실행
#include <algorithm>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#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());
std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
std::cout << "Contents of the list: ";
for(auto n: l) std::cout << n << ' ';
std::cout << '\n';
std::cout << "Contents of the list, shuffled: ";
for(auto i: v) std::cout << *i << ' ';
std::cout << '\n';
}
Possible output:
Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5
Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2 5
See also
| assigns a range of elements a certain value (function template) | |
| saves the result of a function in a range (function template) |