Namespaces
Variants

std::ranges:: iota, std::ranges:: iota_result

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
Constrained algorithms
All names in this menu belong to namespace 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
헤더 파일에 정의됨 <numeric>
호출 시그니처
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 >

iota ( O first, S last, T value ) ;
(1) (C++23부터)
template < std:: weakly_incrementable T, ranges:: output_range < const T & > R >

constexpr iota_result < ranges:: borrowed_iterator_t < R > , T >

iota ( R && r, T value ) ;
(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

참고 항목

범위 내 모든 요소에 주어진 값을 복사-할당함
(함수 템플릿)
요소 범위에 특정 값을 할당함
(알고리즘 함수 객체)
연속적인 함수 호출 결과를 범위 내 모든 요소에 할당함
(함수 템플릿)
함수 결과를 범위에 저장함
(알고리즘 함수 객체)
초기값을 반복적으로 증가시켜 생성된 시퀀스로 구성된 view
(클래스 템플릿) (커스터마이제이션 포인트 객체)
(C++11)
범위를 시작값의 연속적인 증가값으로 채움
(함수 템플릿)