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
62
63
  #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;
}


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
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <algorithm>
#include <iomanip>

struct customer
{
    customer( int age, std::string name ) : age(age), name( std::move(name) ) {}

    int age ; std::string name ;

    friend std::ostream& operator<< ( std::ostream& stm, const customer& cust )
    { return stm << "customer{ " << cust.age << ", " << std::quoted(cust.name) << " }" ; }
};

int main()
{
    std::vector< std::shared_ptr<customer> > seq
    {
        std::make_shared<customer>( 12, "abcd" ),
        std::make_shared<customer>( 34, "efgh" ),
        std::make_shared<customer>( 12, "ijkl" ),
        std::shared_ptr<customer>{nullptr},
        std::make_shared<customer>( 56, "mnop" ),
        std::make_shared<customer>( 12, "qrst" ),
        std::make_shared<customer>( 78, "uvwx" )
    };

    int age ;
    std::cout << "age to be erased? " ;
    std::cin >> age ; // enter 12
    std::cout << "remove customers with age == " << age << '\n' ;

    // capture age by value
    const auto age_equals = [age] ( const auto& ptr ) { return ptr && ptr->age == age ; } ;
    
    seq.erase( std::remove_if( std::begin(seq), std::end(seq), age_equals ), std::end(seq) ) ;

    for( const auto& ptr : seq )
    {
        if(ptr) std::cout << *ptr.get() << "  " ;
        else std::cout << "<nullptr>  " ;
    }
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/586797bc09e42e4b

Re. the original code:
Note that std::unary_function is no longer a part of standard C++ (removed in C++17)
http://en.cppreference.com/w/cpp/utility/functional/unary_function
Topic archived. No new replies allowed.