std::ranges:: iota, std::ranges:: iota_result
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical special functions (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical constants (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic linear algebra algorithms (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data-parallel types (SIMD) (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Floating-point environment (C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric array (
valarray
)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더 파일에 정의됨
<numeric>
|
||
|
호출 시그니처
|
||
|
template
<
std::
input_or_output_iterator
O,
std::
sentinel_for
<
O
>
S,
std::
weakly_incrementable
T
>
|
(1) | (C++23부터) |
|
template
<
std::
weakly_incrementable
T,
ranges::
output_range
<
const
T
&
>
R
>
constexpr
iota_result
<
ranges::
borrowed_iterator_t
<
R
>
, T
>
|
(2) | (C++23부터) |
|
헬퍼 타입
|
||
|
template
<
class
O,
class
T
>
using iota_result = ranges:: out_value_result < O, T > ; |
(3) | (C++23부터) |
범위
[
first
,
last
)
를 순차적으로 증가하는 값으로 채웁니다. 시작 값은
value
이며, 반복적으로
++
value
를 평가합니다.
동등한 연산:
*(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
참고 항목
|
범위 내 모든 요소에 주어진 값을 복사-할당함
(함수 템플릿) |
|
|
(C++20)
|
요소 범위에 특정 값을 할당함
(알고리즘 함수 객체) |
|
연속적인 함수 호출 결과를 범위 내 모든 요소에 할당함
(함수 템플릿) |
|
|
(C++20)
|
함수 결과를 범위에 저장함
(알고리즘 함수 객체) |
|
(C++20)
|
초기값을 반복적으로 증가시켜 생성된 시퀀스로 구성된
view
(클래스 템플릿) (커스터마이제이션 포인트 객체) |
|
(C++11)
|
범위를 시작값의 연속적인 증가값으로 채움
(함수 템플릿) |