Advertisement
avr39ripe

PV913STLAdvExample

Aug 17th, 2020
1,416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5.  
  6. void prn(int val) { std::cout << val << ' '; };
  7.  
  8. int main()
  9. {
  10.     std::vector<int> arr{ 1,2,3,4,5,6,7,8,9,10 };
  11.     std::vector<int> arrB{10,20,30,40,50};
  12.     //std::vector<float> arrF{ 1.1,2.2,3.3,4.3,5.5,6.6,7.7,8.8,9.8,10.1 };
  13.     std::copy_if(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "),
  14.                                             [](const auto& el) { return (el % 2 == 0); });
  15.     std::cout << "\narrB pre copy\n";
  16.     //std::for_each(arrB.begin(), arrB.end(), [](const auto& val) { std::cout << val << ' '; });
  17.     std::copy(arrB.begin(), arrB.end(), std::ostream_iterator<int>(std::cout, " "));
  18.  
  19.     std::copy_if(arr.begin(), arr.end(), std::inserter(arrB, std::next(arrB.begin(), 3)),
  20.                                                 [](const auto& el) { return (el % 2); });
  21.  
  22.     std::cout << "\narrB post copy\n";
  23.     //std::for_each(arrB.begin(), arrB.end(), [](const auto& val) { std::cout << val << ' '; });
  24.     std::copy(arrB.begin(), arrB.end(), std::ostream_iterator<int>(std::cout, " "));
  25.  
  26.     return 0;
  27.  
  28.     //for (int i{ 0 }; i < arr.size(); ++i)
  29.     //{
  30.     //  std::cout << arr[i] << ' ';
  31.     //}
  32.     //std::cout << '\n';
  33.  
  34.     //for (int cnt{0}; const auto& el : arr)
  35.     //{
  36.     //  std::cout << cnt++ << " = " << el << '\n';
  37.     //}
  38.  
  39.     //for (auto it{ std::next(arr.begin(),1) }; it != std::prev(arr.end(),2); ++it)
  40.     //{
  41.     //  std::cout << std::distance(arr.begin(), it) << ' ' << *it << '\n';
  42.     //}
  43.     //std::cout << '\n';
  44.    
  45.     int mul{ 2 };
  46.  
  47.     auto print = [](const auto& val) {std::cout << val << ' '; };
  48.     std::for_each(std::next(arr.begin(), 2), std::prev(arr.end(), 2), [mul](auto& val) { val *= mul; std::cout << val << ' '; });
  49.     //std::for_each(arr.begin(), arr.end(), print);
  50.     std::cout << '\n';
  51.     //std::for_each(arrF.begin(), arrF.end(), print);
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement