STL find() ,还是挺重要的

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;
}

 

http://www.cplusplus.com/reference/algorithm/find/


// find example
#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector

int main () {
  int myints[] = { 10, 20, 30 ,40 };
  int * p;

  // pointer to array element:
  p = std::find (myints,myints+4,30);
  ++p;
  std::cout << "The element following 30 is " << *p << '\n';

  std::vector<int> myvector (myints,myints+4);
  std::vector<int>::iterator it;

  // iterator to vector element:
  it = find (myvector.begin(), myvector.end(), 30);
  ++it;
  std::cout << "The element following 30 is " << *it << '\n';

  return 0;
}



Output:
The element following 30 is 40
The element following 30 is 40


posted @ 2013-07-04 16:55  scott_h  阅读(215)  评论(0编辑  收藏  举报