std::find()
Searches for the first occurrence of a specified value within a given range `[first, last)`.
std::find(first, last, value);This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Searches for the first occurrence of a specified value within a given range `[first, last)`.
The `std::find()` algorithm, found in the `<algorithm>` header, is used to locate the first element in a range that is equal to a specified value. It takes three arguments: `first` (an iterator to the beginning of the range), `last` (an iterator to one past the end of the range), and `value` (the value to search for). It returns an iterator to the first element that compares equal to `value`. If no such element is found, it returns the `last` iterator. The comparison is performed using `operator==`. `std::find()` has a linear time complexity, O(N), where N is the number of elements in the range, because it may need to examine every element in the worst case. It works with any input iterator, making it highly versatile across various container types (vectors, lists, arrays, etc.). While efficient for small collections or when elements are likely to be found early, for very large, sorted collections, `std::earch()` or `std::ound()` offer logarithmic time complexity (O(log N)) and are generally preferred. `std::find()` is a fundamental building block for many search-related operations and is often combined with other algorithms or container methods.
Quick reference
Syntax
std::find(first, last, value);
Inputs
Parameters
See it in practice
Examples
Finding an integer in a vector
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {10, 20, 30, 40, 50};
int search_value = 30;
auto it = std::find(numbers.begin(), numbers.end(), search_value);
if (it != numbers.end()) {
std::cout << "Value " << search_value << " found at index: " << std::distance(numbers.begin(), it) << std::endl;
} else {
std::cout << "Value " << search_value << " not found." << std::endl;
}
return 0;
}Value 30 found at index: 2
`std::find()` searches for 30. Since it's found, an iterator pointing to 30 is returned, and its index is calculated using `std::distance()`.
Handling value not found
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<std::string> fruits = {"apple", "banana", "cherry"};
std::string search_fruit = "grape";
auto it = std::find(fruits.begin(), fruits.end(), search_fruit);
if (it != fruits.end()) {
std::cout << "Fruit " << search_fruit << " found." << std::endl;
} else {
std::cout << "Fruit " << search_fruit << " not found." << std::endl;
}
return 0;
}Fruit grape not found.
When 'grape' is not in the vector, `std::find()` returns `fruits.end()`, indicating the value was not found.
Finding an element in a raw array
#include <iostream>
#include <algorithm>
int main() {
char chars[] = {'a', 'b', 'c', 'd', 'e'};
char search_char = 'c';
int n = sizeof(chars) / sizeof(chars[0]);
char* ptr = std::find(chars, chars + n, search_char);
if (ptr != chars + n) {
std::cout << "Character '" << search_char << "' found at position: " << (ptr - chars) << std::endl;
} else {
std::cout << "Character '" << search_char << "' not found." << std::endl;
}
return 0;
}Character 'c' found at position: 2
`std::find()` works with raw arrays by treating pointers as iterators. Pointer arithmetic is used to find the position.
Debug faster
Common Errors
Logical Error
Cause: Forgetting to check if the returned iterator is equal to the `last` iterator, leading to dereferencing an invalid iterator if the element is not found.
Fix: Always compare the returned iterator with the `last` iterator (`myVector.end()`) to determine if the element was found before dereferencing.
#include <vector>
#include <algorithm>
int main() {
std::vector<int> v = {1, 2};
auto it = std::find(v.begin(), v.end(), 3);
// int val = *it; // Undefined behavior if 3 is not found
return 0;
}Performance Issue
Cause: Using `std::find()` on a large, sorted collection when `std::binary_search()` or `std::lower_bound()` would be more efficient.
Fix: For sorted ranges, use `std::binary_search()` (to check existence) or `std::lower_bound()` (to find position) for O(log N) complexity.
std::vector<int> sorted_data = {1, 5, 8, 12, 15}; // Large sorted vector
std::find(sorted_data.begin(), sorted_data.end(), 12); // O(N) when O(log N) is possibleRuntime support
Compatibility
Source: cppreference.com
Common questions
Frequently Asked Questions
Searches for the first occurrence of a specified value within a given range `[first, last)`.
first: An iterator to the beginning of the range to search. last: An iterator to the end of the range (one past the last element). value: The value to search for.
Logical Error: Always compare the returned iterator with the `last` iterator (`myVector.end()`) to determine if the element was found before dereferencing. Performance Issue: For sorted ranges, use `std::earch()` (to check existence) or `std::ound()` (to find position) for O(log N) complexity.