/
AirLexa
/
06
Обзор
Документация
Войти
/
AirLexa
/
06
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Lesson_03/Task_2/Task_2.cpp
63 строки
1 KB
AirLexa
урок 3
08 июн 2026, 15:25
Верифицирован
08 июн 2026, 15:25
e290502
Код
Авторство
О чём код?
#include <iostream> #include <windows.h> #include <vector> #include <future> #include <thread> #include <algorithm> template<typename Iterator, typename Function> void parallel_for_each(Iterator begin, Iterator end, Function func) { size_t length = std::distance(begin, end); if (length == 0) return; const size_t minBlockSize = 5; if (length <= minBlockSize) { std::for_each(begin, end, func); return; } Iterator middle = begin; std::advance(middle, length / 2); auto future = std::async(std::launch::async, parallel_for_each<Iterator, Function>, begin, middle, func); parallel_for_each(middle, end, func); future.get(); } void print(const std::vector<int>& arr) { for (int value : arr) { std::cout << value << " "; } std::cout << std::endl; } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); std::vector<int> numbers = { 9, 3, 8, 1, 6, 2, 7, 5, 4 }; std::cout << "Исходный массив:" << std::endl; print(numbers); parallel_for_each(numbers.begin(), numbers.end(), [](int& value) { value *= 2; }); std::cout << "\nПосле parallel_for_each (*=2):" << std::endl; print(numbers); return 0; }