anduin ревизій цього gist 1 month ago. До ревизії
1 file changed, 75 insertions
wired.cpp(файл створено)
| @@ -0,0 +1,75 @@ | |||
| 1 | + | #include <iostream> | |
| 2 | + | #include <vector> | |
| 3 | + | #include <array> | |
| 4 | + | #include <string> | |
| 5 | + | #include <algorithm> | |
| 6 | + | #include <ranges> | |
| 7 | + | #include <utility> | |
| 8 | + | ||
| 9 | + | // 1. 正常人类 (C++98) | |
| 10 | + | void print_evens_c98(int arr[], int size) { | |
| 11 | + | for (int i = 0; i < size; i++) { | |
| 12 | + | if (arr[i] % 2 == 0) { | |
| 13 | + | std::cout << arr[i] << " "; | |
| 14 | + | } | |
| 15 | + | } | |
| 16 | + | } | |
| 17 | + | ||
| 18 | + | // 2. 稍微不正常 (C++11) | |
| 19 | + | void print_evens_c11(const std::vector<int>& nums) { | |
| 20 | + | std::for_each(nums.begin(), nums.end(), [](int n) { | |
| 21 | + | if (n % 2 == 0) { | |
| 22 | + | std::cout << n << " "; | |
| 23 | + | } | |
| 24 | + | }); | |
| 25 | + | } | |
| 26 | + | ||
| 27 | + | // 3. 再不正常一点 (C++20) | |
| 28 | + | void print_evens_c20(const std::vector<int>& nums) { | |
| 29 | + | for (int n : nums | std::views::filter([](int n) { return n % 2 == 0; })) { | |
| 30 | + | std::cout << n << " "; | |
| 31 | + | } | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | // 4. 特别不正常 (C++20 纯编译期) | |
| 35 | + | template <size_t... Is> | |
| 36 | + | constexpr auto get_even_numbers_string_helper(const std::array<int, sizeof...(Is)>& arr, std::index_sequence<Is...>) { | |
| 37 | + | auto filter_and_convert = [&](auto index) constexpr { | |
| 38 | + | if (arr[index] % 2 == 0) { | |
| 39 | + | // Returns an empty string literal to avoid runtime heap allocation | |
| 40 | + | return std::to_string(arr[index]) + " "; | |
| 41 | + | } | |
| 42 | + | return std::string(""); | |
| 43 | + | }; | |
| 44 | + | return (filter_and_convert(Is) + ...); | |
| 45 | + | } | |
| 46 | + | ||
| 47 | + | template <size_t N> | |
| 48 | + | void print_even_in_array(const std::array<int, N>& arr) { | |
| 49 | + | std::cout << get_even_numbers_string_helper(arr, std::make_index_sequence<N>{}); | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | int main() { | |
| 53 | + | int arr[] = {5, 2, 8, 1, 6}; | |
| 54 | + | std::vector<int> nums_vec = {5, 2, 8, 1, 6}; | |
| 55 | + | ||
| 56 | + | constexpr std::array<int, 5> nums_arr_constexpr = {5, 2, 8, 1, 6}; | |
| 57 | + | ||
| 58 | + | std::cout << "1. 正常人类 (C++98):" << std::endl; | |
| 59 | + | print_evens_c98(arr, 5); | |
| 60 | + | std::cout << "\n\n"; | |
| 61 | + | ||
| 62 | + | std::cout << "2. 稍微不正常 (C++11):" << std::endl; | |
| 63 | + | print_evens_c11(nums_vec); | |
| 64 | + | std::cout << "\n\n"; | |
| 65 | + | ||
| 66 | + | std::cout << "3. 再不正常一点 (C++20):" << std::endl; | |
| 67 | + | print_evens_c20(nums_vec); | |
| 68 | + | std::cout << "\n\n"; | |
| 69 | + | ||
| 70 | + | std::cout << "4. 特别不正常 (C++20 纯编译期):" << std::endl; | |
| 71 | + | print_even_in_array(nums_arr_constexpr); | |
| 72 | + | std::cout << "\n"; | |
| 73 | + | ||
| 74 | + | return 0; | |
| 75 | + | } | |
Новіше
Пізніше