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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#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;
}
| |