STL algorithm算法find(16)

来源:互联网 发布:中国云计算行业分析 编辑:程序博客网 时间:2024/06/07 02:10
原文地址:http://www.cplusplus.com/reference/algorithm/find/
function template
<algorithm>

std::find

template <class InputIterator, class T>   InputIterator find (InputIterator first, InputIterator last, const T& val);
Find value in range
Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last.

返回范围内与val相等的第一个元素的迭代器,如果没有这样的元素,那么将返回last.

例子:

#include <iostream>#include <algorithm>#include <vector>#include <array>using namespace std;void find2(){    vector<int> vi{1,5,7,8,9,9,8,5,9};    cout<<"vi=";    for(int &i:vi)        cout<<i<<" ";    cout<<endl;    auto it=find(vi.begin(),vi.end(),8);    cout<<"auto it=find(vi.begin(),vi.end(),8);"<<endl;    cout<<"(it-1)="<<*(it-1)<<endl;    cout<<"it="<<*it<<endl;    cout<<"(it+1)="<<*(it+1)<<endl;    auto it2=find(vi.begin(),vi.end(),18);    cout<<"auto it2=find(vi.begin(),vi.end(),18);"<<endl;    cout<<"(it2-1)="<<*(it2-1)<<endl;    cout<<"it2="<<*it2<<endl;    cout<<"(it2+1)="<<*(it2+1)<<endl;    if(it2==vi.end())        cout<<"it2==vi.end()"<<endl;}
运行截图:



The function uses operator== to compare the individual elements to val.

使用operator==来比较元素与val是否相等。


The behavior of this function template is equivalent to:
123456789
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;}



Parameters

first, last
Input iterators to the initial and final positions in a sequence. The range searched is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed bylast.
比较的范围。
val
Value to search for in the range.
T shall be a type supporting comparisons with the elements pointed by InputIterator using operator== (with the elements as left-hand side operands, and val as right-hand side).
比较的值。

Return value

An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.

返回范围内与val相等的第一个元素的迭代器,如果没有这样的元素,那么将返回last.



Example

12345678910111213141516171819202122232425262728
// 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 myints\n";  return 0;}
Edit & Run


Output:
Element found in myints: 30Element found in myvector: 30

Complexity

Up to linear in the distance between first and last: Compares elements until a match is found.

Data races

Some (or all) of the objects in the range [first,last) are accessed (once at most).

Exceptions

Throws if either an element comparison or an operation on an iterator throws.
Note that invalid arguments cause undefined behavior.

——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-11

于GDUT

——————————————————————————————————————————————————————————————————




0 0
原创粉丝点击