My task is:
Write a function that takes a pair of iterators to a vector and an int value. Look for that value in the range and return iterator to requested element.
My implementation for the above task is:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using data = vector<int>;
using iter = data::const_iterator;
iter contains(const data, iter, iter, int);
int main() {
data numbers{1, 2, 5, 7, 9, 14, 18};
iter b_iter = numbers.begin() + 2;
iter e_iter = numbers.end();
iter found = contains(numbers, b_iter, e_iter, 13);
if (found == numbers.end())
cout << "not found" << endl;
else
cout << *found << endl;
return 0;
}
iter contains(const data container, iter b_iter, iter e_iter, int num) {
while (b_iter != e_iter) {
if (*b_iter == num)
return b_iter;
b_iter++;
}
return container.end();
}
As you see, I am iterating from beginning to the end and return if searched value is found. Otherwise, function return iterator to one pass the last element (container.end()
). But this program outputs 0
to the console, where I expect not found
. When updating my function's fist argument to reference to data rather than value like so:
iter contains(const data&, iter, iter, int);
Function works as expected and not found
is printed to the terminal. Why passing data by value doesn't work as expected?