find<>
: Sequential Search, by Equality¶
Basic Usage¶
Search at its simplest: linearly for equality
int int_array_c[] = { 34, 45, 1, 3, 2, 666 };
const int *found = std::find(int_array_c, int_array_c+6, 3);
if (found == int_array_c+6)
std::cout << "not found" << std::endl;
else
std::cout << *found << std::endl;
Attention: “not found” ⇔ pointer one past the last element
Not Found?¶
Important concept: “not found” ⇔ pointer past the last element
std::vector<int> int_array;
// ...
std::vector<int>::const_iterator found =
std::find(int_array.begin(), int_array.end(), 7);
if (found == int_array.end())
std::cout << "not found" << std::endl;
else
std::cout << *found << std::endl;