本文介绍了如何实现find_if和remove_if的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好.
这是我的代码:
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;
}
这段代码还可以,并且可以提供所需的输出,但是我想使用算法标头的find/find_if
和remove_if
函数来实现findID()
和removeID()
函数.是否有可能?如果可能的话,我该如何实施呢?请帮忙.
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.
推荐答案
#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;
}
这篇关于如何实现find_if和remove_if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!