How do you write a program in c plus plus to search a number in an array?

1 answer

Answer

1232850

2026-04-21 04:25

+ Follow

The following function performs a non-recursive linear search of the given vector, returning a constant iterator to the element containing the given value. If the value does not exist, the "one-past-the-end" iterator is returned instead.

std::vector<int>::const_iterator find (int val, const std::vector<int>& v){

for (std::vector<int>::const_iterator it=v.begin(); it!=v.end(); ++it)

if (*it==val) return it;

return v.end();

}

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.