Advertisement
Lavig

Другий семестр. Лабораторна робота №18 (Завдання 2)

May 15th, 2025 (edited)
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. template<typename Iterator, typename Condition>
  8. Iterator findFirstIf(Iterator start, Iterator end, Condition cond) {
  9.     for (Iterator current = start; current != end; ++current) {
  10.         if (cond(*current)) {
  11.             return current;
  12.         }
  13.     }
  14.     return end;
  15. }
  16.  
  17. bool isEven(int x) {
  18.     return x % 2 == 0;
  19. }
  20.  
  21. bool isGreater(int x) {
  22.     return x > 5;
  23. }
  24.  
  25. int main() {
  26.     SetConsoleOutputCP(1251);
  27.     SetConsoleCP(1251);
  28.     const int N = 5;
  29.     int arr[N]{};
  30.     vector<int> vec;
  31.     cout << "Введіть 5 цілих чисел:" << endl;
  32.     for (int i = 0; i < N; i++) {
  33.         cin >> arr[i];
  34.         vec.push_back(arr[i]);
  35.     }
  36.     int* arrResult = findFirstIf(arr, arr + N, isEven);
  37.     if (arrResult != arr + N) {
  38.         cout << "Перший парний елемент: " << *arrResult << ", його індекс - " << (arrResult - arr) << endl;
  39.     }
  40.     else {
  41.         cout << "Парних елементів у масиві немає" << endl;
  42.     }
  43.     auto vecResult = findFirstIf(vec.begin(), vec.end(), isGreater);
  44.     if (vecResult != vec.end()) {
  45.         cout << "Перший елемент більший за 5: " << *vecResult << ", його індекс - " << (vecResult - vec.begin()) << endl;
  46.     }
  47.     else {
  48.         cout << "У масиві немає елементів більших за 5" << endl;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement