c++ - How to identify if vector iterator has reached end of vector? -


i have vector of integers , want iterator point desired number (call 'test').

vector<int> vec; void find(std::vector<int>::iterator iter, int test) {     if((*iter)!=test){ ++iter;} } 

that problematic because iter might hit end of vector (if 'test' not in vector) , i'll run error.

is there anyway can perform like

if(iter!=vec.end()) 

and stop iteration without specifying vector in function? thank you!

you can take standard library approach , pass pair of iterators function:

template <typename iterator> void foo(iterator begin, iterator end,          typename std::iterator_traits<iterator>::value_type test) {   iterator = begin;   ....   if (iterator != end) { ... } } 

then have figure out algorithm find iterator pointing element value equal test. or call std::find , drop function.

auto = std::find(vec.begin(), vec.end(), test); if (it != vec.end()) { .... } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -