vector_pro::find¶
C++
size_type find(const value_type& target, int(compare2)(const value_type &, const value_type &), size_type from = 0, size_type exclude_to = -1) const;
size_type find(const value_type& target, size_type from = 0, size_type exclude_to = -1) const;
iterator_pro<value_type> find(const value_type& target, int(compare2)(const value_type &, const value_type &), iterator_pro<value_type> from, iterator_pro<value_type> exclude_to) const;
iterator_pro<value_type> find(const value_type& target, iterator_pro<value_type> from, iterator_pro<value_type> exclude_to) const;
const_iterator_pro<value_type> find(const value_type& target, int(compare2)(const value_type &, const value_type &), const_iterator_pro<value_type> from, const_iterator_pro<value_type> exclude_to) const;
const_iterator_pro<value_type> find(const value_type& target, const_iterator_pro<value_type> from, const_iterator_pro<value_type> exclude_to) const;
Find an element¶
Returns the position of the first element which is equal to the target .
Please note that you must ensure your
value_typeimplements the==comparison operator, or you must pass a function of the typeint(const value_type &, const value_type &)as a parameter.Also note that calling the function by passing illegal
fromorexclude_towill get an exception.
Parameters¶
target
The target value you are going to find.from,exclude_to
The range of elements we are find the target.
Can besize_type/iterator_pro/const_iterator_pro.
If the types areszie_types , the params have default values0(start of the container) and-1(end of the container).
The range includes all the elements betweenfromandexclude_to, including the element pointed byfrombut not the one pointed byexclude_to.int(compare2)(const value_type &, const value_type &)
To let us understand how to compare twovalue_type.
Return value¶
The position of the first element which is equal to the target .
The type will be same as the params. ( size_type / iterator_pro / const_iterator_pro )
If nothing found, -1 ( size_type ) or the exclude_end ( iterator_pro / const_iterator_pro ) will be returned.
Example¶
C++
// vector_pro::find
#include <iostream>
#include "vector_pro.h"
/**
* Output:
* (1) the nearest 9 is at:: 8
* (2) the nearest 9 is at:: 11
*/
int main ()
{
vector_pro<int> myvector;
// set some values (from 1 to 10)
for (int i=1; i<=10; i++) myvector.push_back(i);
for (int i=10; i>=0; i--) myvector.push_back(i);
// find the element 9 from the begin of container
auto idx = myvector.find (9, myvector.begin(), myvector.end());
std::cout << "(1) the nearest 9 is at:: " << idx.get_idx() << "\n";
// find the element 9 reversely from the end of container
auto idx2 = myvector.find (9, myvector.rbegin(), myvector.rend());
std::cout << "(2) the nearest 9 is at:: " << idx2.get_idx() << "\n";
return 0;
}
Complexity¶
Linear on the number of elements in the range.