C++标准库---find&find_if
2015-01-25 18:35
363 查看
find: 根据equality操作符,循环查找[first,last)内的所有元素,找出第一个匹配"等同(equality)条件"者。如果找到,就返回一个InputIterator指向该元素,否则返回迭代器last。
template<class InputIterator,class T>
InputIterator find(InputIterator first,InputIterator last,const T& value)
find_if:根据指定的pred运算条件(以仿函数表示),循环查找[first,last)内的所有元素,找出第一个令pred运算结果true者。如果找到就返回一个InputIterator指向该元素,否则就返回迭代器last。
template<class InputIterator,class Predicate>
InputIterator find_if(InputIterator first,InputIterator last,Predicate pred)
代码示例:
template<class InputIterator,class T>
InputIterator find(InputIterator first,InputIterator last,const T& value)
find_if:根据指定的pred运算条件(以仿函数表示),循环查找[first,last)内的所有元素,找出第一个令pred运算结果true者。如果找到就返回一个InputIterator指向该元素,否则就返回迭代器last。
template<class InputIterator,class Predicate>
InputIterator find_if(InputIterator first,InputIterator last,Predicate pred)
代码示例:
//vector #include<iostream> #include<vector> #include<algorithm> #include<functional> #include<string> using namespace std; class Student { public: Student(){} Student(string iname,float iscore):name(iname),score(iscore){} ~Student(){} string name; float score; }; class StudentAdapter:public unary_function<Student,bool> { private: string name; public: explicit StudentAdapter(string iname):name(iname){} bool operator()(const Student& student) { return (student.name==name); } }; int main() { vector<Student> admin; vector<Student>::iterator pos; Student stu1("lanzhihui",89.1); Student stu2("wangdan",89.2); Student stu3("wangqian",89.3); admin.push_back(stu1); admin.push_back(stu2); admin.push_back(stu3); for(pos=admin.begin();pos!=admin.end();++pos) { cout<<pos->name<<" "<<pos->score<<endl; } cout<<endl; //实验find_if pos=find_if(admin.begin(),admin.end(),StudentAdapter("lanzhihui")); if(pos!=admin.end()) { cout<<pos->name<<" "<<pos->score<<endl; } //以下实验find vector<int> coll; vector<int>::iterator pos1; for(int i=0;i<10;i++) { coll.push_back(i); } pos1=find(coll.begin(),coll.end(),5); if(pos1!=coll.end()) { cout<<*pos1<<" is find"<<endl; } system("pause"); return 0; }
相关文章推荐
- 'find_if’ is not a member of ‘std'
- 简单的程序诠释C++ STL算法系列: find & find_if
- C++中的vector&find_if
- STL 之find & find_if
- C++标准库---移除性算法之一remove()&remove_if()
- C++标注库---find()&find_if()&search_n()
- find_if 仿函数错误:cannot convert 'this' pointer from 'const CActTaskData' to 'CActTaskData &'
- C++标准库---count()&count_if()
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- C++中的vector&find_if
- 泛型算法系列6:find()&&find_if()
- WIN32_FIND_DATA wfd; if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
- STL应用 vector & find_if
- C++标准库---替换元素replace()&replace_if()
- some commands related to "find"
- 使用STL通用算法find_if()在list中搜索对象
- how to find "oracle在各种linux上的安装手册"
- After restoring database in SQL Server, if you can't use diagram, execute these sentences
- ABAP--如何返回'F4IF_INT_TABLE_VALUE_REQUEST'函数隐含的字段
- find_if和函数对象结合实现对自定义类型vector的查找