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())
运行结果:
If there are something wrong,please point out,thank you very much!
参考资料:[c plus plus].
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].
相关文章推荐
- 安卓学习笔记---Fragmnet使用之add,show,hide,remove以及replace方法
- (原創) 如何使用remove_copy_if() algorithm? (C/C++) (STL)
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- 从零开始学C++之STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- cvc-elt.1: Cannot find the declaration of element 'taglib'的解决方法
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- STL 之find,find_if,find_end,find_first_of
- 如何使用remove_copy_if() algorithm? (C/C++) (STL)
- STL 如何使用list::remove_if
- (使用STL自带的排序函数进行排序7.3.6)POJ 2945 Find the Clones拷贝相同次数的文本出现了多少次()
- Arraylist forEach,removeIf方法的使用
- Find the first intersection of two lists (if the two list are merged at some point)
- 关于在STL容器list中使用find_if 函数
- Unable to find a version of the runtime to run this application解决方法
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
- Replace Matches Reusing Parts of the Match (替换重新使用部分匹配的匹配)
- ifstream,ofstream的使用方法
- CString类常用方法---Replace(),Remove(),Delete(),Find(),ReverseFind(),Insert()
- STL之for_each, find_if,adjacent_find,count_if的使用