Finding a value and counting its appearances answer different questions. Both std::find and std::count come from <algorithm> and accept a half-open iterator range followed by the value to look for. Neither requires a vector to be sorted first.
std::find(first, last, value) returns an iterator to the first matching element. If no match exists, it returns the supplied last iterator. Compare the result with the range endpoint before using *result. The endpoint is a useful “not found” marker precisely because it is outside the searchable range.
std::count(first, last, value) returns how many elements equal the requested value. It returns a number, not an iterator. Using auto for that result lets the compiler select the algorithm's count type. An empty range produces no match and a count of zero.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> zones{2, 5, 2};
auto found = std::find(zones.begin(), zones.end(), 5);
if (found != zones.end()) { std::cout << *found << '\n'; }
auto matches = std::count(zones.begin(), zones.end(), 2);
std::cout << matches << '\n';
}
The example prints 5 and 2. Notice the different return values: find gives a position that must be checked, while count gives a number you can use directly. Your contains helper only needs to report whether a position was found, so it does not need to dereference the iterator. In copies, convert the count to the helper's int return type; the supplied collection is small enough for that conversion.
contains() with std::find and an endpoint comparison. The supplied searches should print Found: 1 0.copies() with std::count. Convert its result to int with static_cast<int> for this small fixture. The probe should print Copies: 3.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.