您的位置:首页 > 其它

STL中remove_if()/find_if()/replace_if()的使用方法(The usages of remove_if()/find_if()/replace_if() )

2012-12-14 18:19 453 查看
In one of my c++ homework, my teacher let us use some algorithms in STL to operate the student's information.To finish the task, I needed learn to use the algorithms of remove_if(),find_if,replace_if.At first,it was hard for me,but after searching some
information on the internet and reading the book carefully,I overcame them at last.

Now I write the usages of them here,hope can help you!

C++ STL Algorithm find_if remove_if replace_if

1 find_if()的使用方法

iter=find_if(first,last,pr);

函数说明:故名思议就是“查找...如果....”,这个函数的功能就是查找满足条件的元素

参数说明:这里first、last是两个迭代器,限定了查找的范围,pr为函数对象(函数名称)

返回说明:算法执行后返回指向序列中第一个使函数对象pr返回ture的元素的迭代器。

即返回一个元素的地址;该元素是查找元素范围内第一个能使pr函数返回ture的元素。

2 remove_if()的使用方法

iter=remove_if(first,last,del);

函数说明:顾名思义:删除 如果。函数功能就是删除满足条件的元素

参数说明:这里的first、last同样是两个迭代器,同样限定了查找的范围,del也是函数对象

返回说明:算法执行后同样返回一个元素的迭代器,不过这个元素是这一序列元素中删除后留下的最后一个元素(详细见参考实例)。

3 replace_if()的使用方法

replace_if(first,last,pr,val);

函数说明:替换所有满足条件的的元素的值、对象为另一个特定的值、对象。

参数说明:first、last两个限定查找范围的迭代器,pr函数对象,val指定的替换对象、值。

实例展示:现在有一个班的同学信息,用以上函数完成一下操作。

1 查找特定学生的成绩(the usage of find_if())

2 删除成绩在60分一下的学生信息(the usage of remove_if())

3 替换(修改)某一学生的信息(the usage of replace_if())

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
string name1,name2;
//定义一个学生类
class student
{
public:
student(string na,string nu,int ma)
{
name=na;
number=nu;
mark=ma;
}
//声明三个友元函数  三个函数分别应用于replace_if、remove_if、find_if
friend bool re(const student & m);
friend bool del(const student & m);
friend bool Find(const student & m);
string getname()
{return name;}
string getnumber()
{return number;}
int getmark()
{return mark;}
private:
string name,number;
int mark;
};

bool re(const student & m)
{

if(m.name==name2)
return true;
else
return false;
}

bool del(const student & m)
{

if(m.mark<60)
return true;
else
return false;
}

bool Find(const student & m)
{

if(m.name==name1)
return true;
else
return false;
}

int main()
{
int i;
vector<student>vect;
vector<student>::iterator iter;
vector<student>::iterator start;

// 增加四个学生信息    并依此放进容器中
student s1("Li ming","001",59),s2("Han mei","002",90),s3("Jim","003",89),s4("Lucy","004",95);
vect.push_back(s1);vect.push_back(s2);vect.push_back(s3);vect.push_back(s4);

//  输出所有学生的信息
cout<<"The original information of the students:"<<endl;
for(i=0;i<vect.size();i++)
cout<<"name: "<<vect[i].getname()<<"  number: "<<vect[i].getnumber()<<"  mark: "<<vect[i].getmark()<<endl;
cout<<endl<<endl;

//  查找特定学生操作 输入要查找的学生名字  显示该学生信息
cout<<"Please input the name of the student who you are want to find:"<<endl;
cin>>name1;
cout<<"The information of the student you want to find is:"<<endl;
iter=find_if(vect.begin(),vect.end(),Find);
cout<<"name : "<<iter->getname()<<"   number:"<<iter->getnumber()<<"   mark:"<<iter->getmark();
cout<<endl<<endl;

//输入特定学生的名字,并把他的信息用s5的信息替换
student s5("zhan hua","001",59);
cout<<"Please input the studnet's name you want update his/her information:"<<endl;
cin>>name2;
replace_if(vect.begin(),vect.end(),re,s5);
for(start=vect.begin();start!=vect.end();start++)
cout<<"name: "<<start->getname()<<"  number: "<<start->getnumber()<<"  mark: "<<start->getmark()<<endl;
cout<<endl<<endl;

// 删除特定条件下的学生成绩 (删除60分以下的学生信息)
cout<<"The information of the students who's mark is more than 60 or equal to 60:"<<endl;
iter=remove_if(vect.begin(),vect.end(),del);
for(start=vect.begin();start!=iter;start++)
cout<<"name: "<<start->getname()<<"  number: "<<start->getnumber()<<"  mark: "<<start->getmark()<<endl;
cout<<endl<<endl;
return 0;
}


运行结果:



If there are something wrong,please point out,thank you very much!

参考资料:[c plus plus].
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐