Functor taking argument

I have a customer class containing Name and Age. I am wrapping the customer class in a shared pointer and made a vector of the shared pointer.
I have taken 3 customers and pushed them into the vector. Now I want to delete a customer whose age will be given at runtime. For that I need to use erase and remove_if. But I am not able to write the code for functor which will take the input age as argument, for checking the age of the customers. Can any body help. Given my code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <vector>
#include <iostream>
#include <memory>
#include <string>
#include <istream>
#include <ostream>
#include <algorithm>
using namespace std;
class customer {
    private:
    //public:
        int Age;
        string Name;
    public:
        customer();
        customer(int x, string y):Age(x),Name(y) {
        }
        ~customer() {
            cout << "Destructor of customer is called" << endl;
        }
        int getAge() const{
            return Age;
        }
        string getName() const{
            return Name;
        }
};

template<typename T>
struct Display : unary_function< const T&, void> {
    void operator()(const T& Obj)const {
        cout << "Name:" << Obj->getName() << endl;
        cout <<  "Age:" << Obj->getAge() << endl;
        cout << "--------------------------------" << endl;
    }
};

typedef shared_ptr<customer> SmartCustomer;
int main() {
    typedef vector<SmartCustomer> CustomerVect;
    CustomerVect V1;
    V1.reserve(1000);
    for(int i=0; i<3; i++) {
       string Name;
       int Age;
       cout << "Name:";
       cin >> Name;
       cout << "Age:";
       cin >> Age;
       V1.push_back(SmartCustomer(new customer(Age,Name)));
       cout << endl;
    }
    for_each(V1.begin(),V1.end(),Display<SmartCustomer>());
    int Age;
    cout << "Age to be deleted is :";
    cin >> Age;
    V1.erase(remove_if(V1.begin(),V1.end(),/*Functor to take the Age as argument*/),V1.end());
    for_each(V1.begin(),V1.end(),Display<SmartCustomer>());
    return 0;
}
Using a lambda for the remove_if is easier:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <vector>
#include <iostream>
#include <memory>
#include <string>
#include <istream>
#include <ostream>
#include <algorithm>

using namespace std;

class customer 
{
private:
  //public:
  int Age;
  string Name;
public:
  //customer();
  customer(int x, string y) :Age(x), Name(y) {
  }
  ~customer() {
    cout << "Destructor of customer is called" << endl;
  }
  int getAge() const {
    return Age;
  }
  string getName() const {
    return Name;
  }
};

template<typename T>
struct Display : unary_function< const T&, void> {
  void operator()(const T& Obj)const {
    cout << "Name:" << Obj->getName() << endl;
    cout << "Age:" << Obj->getAge() << endl;
    cout << "--------------------------------" << endl;
  }
};

typedef shared_ptr<customer> SmartCustomer;

int main() 
{
  typedef vector<SmartCustomer> CustomerVect;
  CustomerVect V1;
  V1.reserve(1000);
  for (int i = 0; i<3; i++) 
  {
    string Name;
    int Age;
    cout << "Name:";
    cin >> Name;
    cout << "Age:";
    cin >> Age;
    V1.push_back(SmartCustomer(new customer(Age, Name)));
    cout << endl;
  }
  for_each(V1.begin(), V1.end(), Display<SmartCustomer>());
  int Age;
  cout << "Age to be deleted is :";
  cin >> Age;
  auto it = remove_if(V1.begin(), V1.end(), [Age](SmartCustomer& cust)
  {
    return Age == (*cust).getAge();
  });
  
  if (it != V1.end())
    V1.erase(it);
  for_each(V1.begin(), V1.end(), Display<SmartCustomer>());
  return 0;
}


Line56: V1.push_back(SmartCustomer(new customer(Age, Name)));
It's better practice to use make_shared instead of new customer(Age, Name)
http://www.cplusplus.com/reference/memory/make_shared/
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-make_shared
Thanks Thomas for the help.
Topic archived. No new replies allowed.