Seal_of_approval

PrAlgo18

Jul 5th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <iterator>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. struct toy
  12. {
  13.     int index;
  14.     string name;
  15.     int price;
  16.     int age;
  17. };
  18.  
  19. /* for_each and lambda unwrap to this function:
  20.  
  21. void my_for_each(vector<toy> toys, void* lambdaf) {
  22.  for (vector<toy>::iterator a = toys.begin(); a != toys.end(); ++a)
  23.     lambdaf(*a, **hooked_kwargs);
  24.  }
  25.  
  26. void lambdaf(toy& a, vecotr<int>& answer, int x, int y) {
  27.     if (a.age >= x && a.age <= y)
  28.         answer.push_back(a.index);
  29.  }
  30.  
  31. void final_unwrap(vector<toy> toys, vector<int>& answer, int x, int y) {
  32.     for (vector<toy>::iterator it = toys.begin(); it != toys.end(); ++it) {
  33.         if ((*it).age >= x && (*it).age <= y)
  34.             answer.push_back((*it).index);
  35.     }
  36. }
  37.  */
  38.  
  39. int main(void)
  40. {
  41.     ifstream in("/Users/masha/Documents/PrAlgo18/PrAlgo18/input");
  42.     //ifstream in("input.txt");
  43.     //ofstream out("output.txt");
  44.     vector<toy> toys;
  45.     int x,y,n;
  46.     in >> n;
  47.    
  48.     toy t;
  49.     for (int i = 0; i < n; i++)
  50.     {
  51.         in >> t.name >> t.price >> t.age;
  52.         t.index = i + 1;
  53.         toys.push_back(t);
  54.     }
  55.    
  56.     in >> x >> y;
  57.    
  58.     vector<int> answer;
  59.    
  60.  
  61.     for_each(toys.begin(), toys.end(), [&answer, x, y](toy a) { if (a.age >= x && a.age <= y) answer.push_back(a.index);});
  62.  
  63.     cout << answer.size() << endl;
  64.     for (int i = 0; i < answer.size(); i++) cout << answer[i] << endl;
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment