C++语法基础--泛型算法(generic algorithm)--find(),find_if()
来源:互联网 发布:淘宝客怎么注册买家 编辑:程序博客网 时间:2024/06/06 22:59
2.大多数算法是通过遍历由两个迭代器标记的一段元素来实现其功能,典型情况下,算法在遍历一段元素范围时,操纵其中的每一个元素。
3.find()
只要找到与给定值相等的元素,find就会返回指向该元素的迭代器
原型:
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
等效原理:
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
eg:
int main(void)
{
int myints[] = { 10, 20, 30 ,40 };
int * p;
// pointer to array element:
p = find (myints,myints+4,30);
++p;
cout << *p << '\n';//40
vector<int> myvector (myints,myints+4);
vector<int>::iterator it;
// iterator to vector element:
it = find (myvector.begin(), myvector.end(), 30);
++it;
cout<< *it << '\n';//40
return 0;
}
4.迭代算法和容器绑定起来
要求:
*遍历容器。
*元素类型支持==操作符,否则需要额外提供比较函数
eg:如果class A 没有重载==操作符,则程序编译失败
class A
{
public:
A(int i):x(i){}
void f(){cout<<x<<endl;}
bool operator==(A b) const
{
return x==b.x;
}
private:
int x;
};
int main(void)
{
A a(1),b(2),c(3);
vector<A> myvector ;
myvector.push_back(a);
myvector.push_back(b);
myvector.push_back(c);
vector<A>::iterator it;
// iterator to vector element:
it = find (myvector.begin(), myvector.end(), b);
it->f();//2
return 0;
}
4.find_if
Returns an iterator to the first element in the range [first,last) for which pred returns true.
If no such element is found, the function returns last.
原型:
template <class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
该算法等效于:
template<class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return first;
++first;
}
return last;
}
解析:
first, last:
Input iterators to the initial and final positions in a sequence.
pred :
Unary function that accepts an element in the range as argument and returns a value convertible to bool.
Example:
bool IsEven (int i) {
return ((i%2)==0);
}
int main ()
{
int arr[]={1,2,3,2,1};
vector<int> myvector(arr,arr+5);
vector<int>::iterator it ;
it=std::find_if (myvector.begin(), myvector.end(), IsEven);
cout << "The first Even value is " << *it << '\n';
return 0;
}
Output:The first Even value is 2
- C++语法基础--泛型算法(generic algorithm)--find(),find_if()
- C++语法基础--泛型算法(generic algorithm)--transform
- C++语法基础--泛型算法(generic algorithm)--iostream迭代器
- C++语法基础--泛型算法(generic algorithm)--只读算法accumulate(),find_first_of(),count(),count_if()
- C++语法基础--泛型算法(generic algorithm)--写入容器元素的算法fill(),fill_n,replace()
- C++语法基础--泛型算法(generic algorithm)--对容器排序的算法sort(),stable_sort(),unique()
- C++语法基础--泛型算法(generic algorithm)--replace_copy(),unique_copy,copy()
- C++语法基础--泛型算法(generic algorithm)--反向迭代器,reverse_iterator::base()
- C++语法基础--泛型算法(generic algorithm)--插入迭代器back_inserter(),front_insertor(),inserter()以及next()函数简介
- 泛型算法系列6:find()&&find_if()
- 泛型算法find和find_if以及sort
- STL algorithm算法find_if(19)
- c++ stl algorithm: std::find, std::find_if
- c++ stl algorithm: std::find, std::find_if
- STL算法find_if和find
- find,find_if
- c++:algorithm-find
- A Generic - Reusable Diff Algorithm in C#.
- Python中的随机函数
- 十款超高人气FTP客户端软件横评(一)
- Android之ResolveInfo
- request.getSession(false)
- 【rzxt】详细了解taskmgr.exe进程的基本信息
- C++语法基础--泛型算法(generic algorithm)--find(),find_if()
- 正确的理解this 和 super
- 搭建自己的虚拟openerp环境
- Android 系统编译顺序
- NALU(NAL单元)的顺序要求
- Cactus入门
- 点击打印出现IE已经阻止此站点用不安全方式使用 ActiveX 控件解决方
- MySQL-Cluster架构图文详解
- uva 12345 (分段 / 树套树(好题))