Seredenko-V

lambda

May 29th, 2023
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <vector>
  5. #include <numeric>
  6. #include <algorithm>
  7. #include <execution>
  8. #include <ctime>
  9. #include <functional>
  10.  
  11. using namespace std;
  12.  
  13. int main() {
  14.  
  15.     vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  16.     int sum = accumulate(v.begin(), v.end(), 0);
  17.     cout << sum << endl;
  18.     map<string, int> peoples{{"First"s, 2}, {"Second"s, 8}, {"Ivan"s, 35}};
  19.     int result = accumulate(peoples.begin(), peoples.end(), 0, [](int sum, pair<string, int> people) {
  20.         return sum += people.second;
  21.     });
  22.     cout << result << endl;
  23.     cout << "========================="s << endl;
  24.     vector<int> elements(100'000'000u);
  25.     iota(elements.begin(), elements.end(), 0);
  26.     clock_t begin = clock();
  27.     for_each(execution::par, elements.begin(), elements.end(), [](int& elem) {
  28.         elem *= 10;
  29.     });
  30.     clock_t end = clock();
  31.     cout << "time for_each "s << end - begin << " ms"s << endl;
  32.  
  33.     const double PI = 3.14;
  34.     int value = 5;
  35.  
  36.  
  37.     function<void(int&)> func = [&value](int& elem) {
  38.         value = 10;
  39.         //elem = 16;
  40.     };
  41.  
  42.  
  43.     func(value);
  44.  
  45.     cout << value << endl;
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment