std::find
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
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:
|
|
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 usingoperator==
(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
|
|
Output:
Element found in myints: 30
Element 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
——————————————————————————————————————————————————————————————————