在给定的数字范围内查找元素。返回一个迭代器,该迭代器在[first,last)范围内比较等于val的第一个元素。如果找不到这样的元素,则该函数最后返回。
函数模板:
InputIterator find (InputIterator first, InputIterator last, const T& val)
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 by last.
val :
Value to be search in the range
Return Value :
An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.
例子:
Input : 10 20 30 40
Output : Element 30 found at position : 2 (counting from zero)
Input : 8 5 9 2 7 1 3 10
Output : Element 4 not found.
// CPP program to illustrate
// std::find
// CPP program to illustrate
// std::find
#include
int main ()
{
std::vector vec { 10, 20, 30, 40 };
// Iterator used to store the position
// of searched element
std::vector::iterator it;
// Print Original Vector
std::cout << "Original vector :";
for (int i=0; i
输出:
Original vector : 10 20 30 40
Element 30 found at position : 2 (counting from zero)
相关文章:
- std :: search
- std :: find_if,std :: find_if_not
- std :: nth_element
- std :: find_end
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。