std::ranges::take_view<V>::take_view
cppreference.net에서
take_view() requires std::default_initializable<V> = default;
|
(1) | (C++20부터) |
constexpr explicit take_view( V base, ranges::range_difference_t<V> count );
|
(2) | (C++20부터) |
take_view을(를) 생성합니다.
1) 기본 생성자입니다. 기본 뷰 를 값으로 초기화하고
base_ count_을(를) 0으(로) 초기화합니다. 생성 후, base()은(는) V()의 복사본을 반환하고 size()은(는) 0을(를) 반환합니다.2) 기본 뷰
base_를 std::move(base)으(로) 초기화하고 count_을(를) count으(로) 초기화합니다. 생성 후, base()은(는) base의 복사본을 반환하고 size()은(는) count과(와) ranges::size(base) 중 더 작은 값을 반환합니다.매개변수
| base | - | 기본 뷰 |
| count | - | 취할 요소의 개수 |
예제
에라토스테네스의 체 방법을 사용하여 생성된 첫 번째 n 개의 소수를 출력합니다.
이 코드 실행
#include <bit> #include <bitset> #include <iomanip> #include <iostream> #include <limits> #include <ranges> constexpr unsigned clog2(auto x) // ≈ ⌈ log₂(x) ⌉ { return std::numeric_limits<decltype(x)>::digits - std::countl_zero(x); } template<unsigned Count> struct FirstPrimes { static constexpr int count = Count; constexpr bool operator()(int n) // is prime? { return n < 2 ? false : n == 2 ? true : n % 2 == 0 or bits_.test(n / 2) ? false : true; } private: consteval static auto init() { std::bitset<size_ / 2 + 1> bits; for (int n{3}; n < size_; n += 2) for (int i{n}, j{3}, k{}; (k = i * j) < size_; j += 2) bits.set(k / 2); return bits; } // Keep only odd numbers; 0 means it is a prime constexpr static auto bits_ { init() }; // a(n) <= n * (log(n) + log(log(n))) static constexpr int size_ = Count * (clog2(Count) + clog2(clog2(Count))); }; int main() { constexpr FirstPrimes<42> primes; auto primes_view = std::ranges::take_view{ std::views::iota(1) | std::views::filter(primes) , primes.count }; std::cout << "First " << primes.count << " prime numbers are:\n"; for (int new_line{1}; const int prime : primes_view) std::cout << std::setw(3) << prime << (new_line++ % 7 ? ' ' : '\n'); }
출력:
First 42 prime numbers are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181
결함 보고서
다음 동작 변경 결함 보고서는 이전에 게시된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
|
LWG 3714
( P2711R1 ) |
C++20 | 다중 매개변수 생성자가 explicit이 아니었음 | explicit으로 변경됨 |