C++每日一练(STL算法——find)

来源:互联网 发布:怎样查看淘宝购买记录 编辑:程序博客网 时间:2024/06/06 08:34

一、今日课题

find

二、实战演练

template <class InputIterator, class T>InputIterator find (InputIterator first, InputIterator last, const T& val);

1)有何用?

利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的一个InputIterator。

2)怎么用?

// find example#include <iostream>     // std::cout#include <algorithm>    // std::find#include <vector>       // std::vectorint main () {  // using std::find with array and pointer:  int myints[] = { 10, 20, 30, 40 };  int * p;  p = std::find (myints, myints+4, 30);  if (p != myints+4)    std::cout << "Element found in myints: " << *p << '\n';  else    std::cout << "Element not found in myints\n";  // using std::find with vector and iterator:  std::vector<int> myvector (myints,myints+4);  std::vector<int>::iterator it;  it = find (myvector.begin(), myvector.end(), 30);  if (it != myvector.end())    std::cout << "Element found in myvector: " << *it << '\n';  else    std::cout << "Element not found in myvector\n";  return 0;}

三、C++树

这里写图片描述


10/28/2016 5:46:07 PM

0 0
原创粉丝点击