CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sat, 19 Jul 2025 19:17:31 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/algorithm/search/
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 19 Jul 2025 19:17:31 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"aebc-l2mJ3RE7nt8AzHIehpZcKIDKiZA"
Content-Encoding: gzip
Searches the range
The elements in both ranges are compared sequentially using
This function returns the first of such occurrences. For an algorithm that returns the last instead, see find_end.
The behavior of this function template is equivalent to:
If the sequence is not found, the function returns last1.
Output:
Note that invalid arguments cause undefined behavior.
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<algorithm>
- adjacent_find
-
all_ofC++11
-
any_ofC++11
- binary_search
- copy
- copy_backward
-
copy_ifC++11
-
copy_nC++11
- count
- count_if
- equal
- equal_range
- fill
- fill_n
- find
- find_end
- find_first_of
- find_if
-
find_if_notC++11
- for_each
- generate
- generate_n
- includes
- inplace_merge
-
is_heapC++11
-
is_heap_untilC++11
-
is_partitionedC++11
-
is_permutationC++11
-
is_sortedC++11
-
is_sorted_untilC++11
- iter_swap
- lexicographical_compare
- lower_bound
- make_heap
- max
- max_element
- merge
- min
- min_element
-
minmaxC++11
-
minmax_elementC++11
- mismatch
-
moveC++11
-
move_backwardC++11
- next_permutation
-
none_ofC++11
- nth_element
- partial_sort
- partial_sort_copy
- partition
-
partition_copyC++11
-
partition_pointC++11
- pop_heap
- prev_permutation
- push_heap
- random_shuffle
- remove
- remove_copy
- remove_copy_if
- remove_if
- replace
- replace_copy
- replace_copy_if
- replace_if
- reverse
- reverse_copy
- rotate
- rotate_copy
- search
- search_n
- set_difference
- set_intersection
- set_symmetric_difference
- set_union
-
shuffleC++11
- sort
- sort_heap
- stable_partition
- stable_sort
- swap
- swap_ranges
- transform
- unique
- unique_copy
- upper_bound
- Reference
- <algorithm>
- search
function template
<algorithm>
std::search
equality (1) | template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); |
---|---|
predicate (2) | template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); |
Search range for subsequence
[first1,last1)
for the first occurrence of the sequence defined by [first2,last2)
, and returns an iterator to its first element, or last1 if no occurrences are found.The elements in both ranges are compared sequentially using
operator==
(or pred, in version (2)): A subsequence of [first1,last1)
is considered a match only when this is true
for all the elements of [first2,last2)
.This function returns the first of such occurrences. For an algorithm that returns the last instead, see find_end.
The behavior of this function template is equivalent to:
|
|
Parameters
- first1, last1
- Forward iterators to the initial and final positions of the searched sequence. The range used is
[first1,last1)
, which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1. - first2, last2
- Forward iterators to the initial and final positions of the sequence to be searched for. The range used is
[first2,last2)
.
For (1), the elements in both ranges shall be of types comparable usingoperator==
(with the elements of the first range as left-hand side operands, and those of the second as right-hand side operands).
- pred
- Binary function that accepts two elements as arguments (one of each of the two sequences, in the same order), and returns a value convertible to
bool
. The returned value indicates whether the elements are considered to match in the context of this function.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
Return value
An iterator to the first element of the first occurrence of[first2,last2)
in [first1,last1)
.If the sequence is not found, the function returns last1.
If
[first2,last2)
is an empty range, the result is unspecified.
If
[first2,last2)
is an empty range, the function returns first1.Example
|
|
Output:
needle1 found at position 3 needle2 not found |
Complexity
Up to linear incount1*count2
(where countX is the distance between firstX and lastX): Compares elements until a matching subsequence is found.Data races
Some (or all) of the objects in both ranges are accessed (possibly more than once).Exceptions
Throws if any of the element comparisons (or pred) throws or if any of the operations on iterators throws.Note that invalid arguments cause undefined behavior.
See also
- find_end
- Find last subsequence in range (function template)
- search_n
- Search range for elements (function template)
- equal
- Test whether the elements in two ranges are equal (function template)
- mismatch
- Return first position where two ranges differ (function template)
- find
- Find value in range (function template)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us