Click here to Skip to main content
15,891,730 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello.
Here is my code :

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

struct Person
{
        string Name;
        string ID;
};

class mainClass
{
        private :
                vector <Person> Vector;
        
        public :
                mainClass() { }
                void storeVector();
                void displayVector();
                void findID();
                void removeID();
};

void mainClass :: storeVector()
{
        Person Obj1;
        Obj1.Name = "Tapas";
        Obj1.ID = "1";
        Vector.push_back(Obj1);
        
        Person Obj2;
        Obj2.Name = "Ashok";
        Obj2.ID = "2";
        Vector.push_back(Obj2);
        
        Person Obj3;
        Obj3.Name = "Arnab";
        Obj3.ID = "3";
        Vector.push_back(Obj3);
        
        Person Obj4;
        Obj4.Name = "Sayantan";
        Obj4.ID = "4";
        Vector.push_back(Obj4);
        
        Person Obj5;
        Obj5.Name = "Mrinal";
        Obj5.ID = "5";
        Vector.push_back(Obj5);
}

void mainClass :: displayVector()
{
        for (vector <Person> :: iterator it = Vector.begin(); it != Vector.end(); it++)
        {
                cout << "  Name : " << it->Name << ", ID : " << it->ID << endl;
        }
}

void mainClass :: findID()
{
        string ID;
        
        cout << "  Enter the ID to find : ";
        cin >> ID;
        
        vector <Person> :: iterator it;
        
        for (it = Vector.begin(); it != Vector.end(); it++)
        {
                if (ID == it->ID)
                {
                        break;
                }
        }
        
        if (it != Vector.end())
        {
                cout << "  Specified ID found." << endl;
                cout << "  Name : " << it->Name << ", ID : " << it->ID << endl;
        }
        else
        {
                cout << "  Specified ID not found." << endl;    
        }
}

void mainClass :: removeID()
{
        string ID;
        
        cout << "  Enter the ID to remove : ";
        cin >> ID;
        
        vector <Person> :: iterator it;
        
        for (it = Vector.begin(); it != Vector.end(); it++)
        {
                if (ID == it->ID)
                {
                        break;
                }
        }
        
        if (it != Vector.end())
        {
                cout << "  Specified ID found." << endl;
                cout << "  Name : " << it->Name << ", ID : " << it->ID << endl;
                Vector.erase(it);
        }
        else
        {
                cout << "  Specified ID not found." << endl;    
        }
}

int main()
{
        mainClass Obj;
        
        Obj.storeVector();
        Obj.displayVector();
        Obj.findID();
        Obj.removeID();
        Obj.displayVector();
                
        return 0;
}

This code is okay and gives desired output, but I want to implement findID() and removeID() function using find/find_if and remove_if function of algorithm header. Is it possible? If possible, then how can I implement it? Please help.
Posted
Updated 7-Mar-10 20:52pm
v7

1 solution

I have solved it by my own. Here it is :
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <iterator>

using namespace std;

struct Person
{

	string Name;
	string ID;
};

class mainClass
{
	private :
		vector <Person> Vector;
	
	public :
		mainClass() { }
		void storeVector();
		void displayVector();
		void findID();
		void removeID();
		
		friend ostream & operator << (ostream &, const Person &);
};

struct IDnumber : public binary_function <Person, string, bool> 
{
	bool operator () ( const Person &Obj, const string &ID ) const 
	{
    		return (Obj.ID == ID);
	}
};

ostream & operator << (ostream &stream, const Person &Obj)
{
	return stream << "  Name : " << Obj.Name << ", ID : " << Obj.ID;
}

void mainClass :: storeVector()
{
	Person Obj1;
	Obj1.Name = "Tapas";
	Obj1.ID = "1";
	Vector.push_back(Obj1);
	
	Person Obj2;
	Obj2.Name = "Ashok";
	Obj2.ID = "2";
	Vector.push_back(Obj2);
	
	Person Obj3;
	Obj3.Name = "Arnab";
	Obj3.ID = "3";
	Vector.push_back(Obj3);
	
	Person Obj4;
	Obj4.Name = "Sayantan";
	Obj4.ID = "4";
	Vector.push_back(Obj4);
	
	Person Obj5;
	Obj5.Name = "Mrinal";
	Obj5.ID = "5";
	Vector.push_back(Obj5);
}

void mainClass :: displayVector()
{	
	copy(Vector.begin(), Vector.end(), ostream_iterator <Person> (cout, "\n"));
}

void mainClass :: findID()
{
	string ID;
	
	cout << endl;
	cout << "  Enter the ID to find : ";
	cin >> ID;
	
	vector <Person> :: iterator it;
	
	it = find_if(Vector.begin(), Vector.end(), bind2nd(IDnumber(), ID));
	
	if (it != Vector.end())
	{
		cout << "  Specified ID found." << endl;
		cout << "  Name : " << it->Name << ", ID : " << it->ID << endl;
	}
	else
	{
		cout << "  Specified ID not found." << endl;	
	}
}

void mainClass :: removeID()
{
	string ID;
	
	cout << endl;
	cout << "  Enter the ID to remove : ";
	cin >> ID;
	
	vector <Person> :: iterator it;
	
	it = find_if(Vector.begin(), Vector.end(), bind2nd(IDnumber(), ID));
	
	if (it != Vector.end())
	{
		cout << "  Specified ID found." << endl;
		cout << "  Name : " << it->Name << ", ID : " << it->ID << endl;
		Vector.erase(it);
		cout << "  Data successfully deleted." << endl << endl;
	}
	else
	{
		cout << "  Specified ID not found." << endl;	
	}
}

int main()
{
	mainClass Obj;
	
	Obj.storeVector();
	Obj.displayVector();
	Obj.findID();
	Obj.removeID();
	Obj.displayVector();
		
	return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900