Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/functional/default searcher"

From cppreference.com
< cpp‎ | utility‎ | functional
m (Example)
m (~)
 
(6 intermediate revisions by 5 users not shown)
Line 3: Line 3:
  
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | functional }}
+
{{dcl header|functional}}
{{dcl | since=c++17 |
+
{{dcl|since=c++17|
 
template< class ForwardIt, class BinaryPredicate {{=}} std::equal_to<> >
 
template< class ForwardIt, class BinaryPredicate {{=}} std::equal_to<> >
 
class default_searcher;
 
class default_searcher;
Line 10: Line 10:
 
{{dcl end}}
 
{{dcl end}}
  
A class suitable for use with {{concept|Searcher}} overload of {{lc|std::search}} that delegates the search operation to the pre-C++17 standard library's {{lc|std::search}}.
+
A class suitable for use with {{named req|Searcher}} overload of {{lc|std::search}} that delegates the search operation to the pre-C++17 standard library's {{lc|std::search}}.
  
{{tt|default_searcher}} is {{concept|CopyConstructible}} and {{concept|CopyAssignable}}.
+
{{tt|std::default_searcher}} is {{named req|CopyConstructible}} and {{named req|CopyAssignable}}.
  
 
===Member functions===
 
===Member functions===
{{member | {{small|std::default_searcher::}}default_searcher | 2=
+
{{member|{{small|std::default_searcher::}}default_searcher|2=
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl |  
+
{{dcla|since=c++17|constexpr=c++20|1=
 
default_searcher( ForwardIt pat_first,
 
default_searcher( ForwardIt pat_first,
 
                   ForwardIt pat_last,
 
                   ForwardIt pat_last,
                   BinaryPredicate pred {{=}} BinaryPredicate());
+
                   BinaryPredicate pred = BinaryPredicate() );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Constructs a {{tt|default_searcher}} by storing copies of {{tt|pat_first}}, {{tt|pat_last}}, and {{tt|pred}}  
+
Constructs a {{tt|std::default_searcher}} by storing copies of {{c|pat_first}}, {{c|pat_last}}, and {{c|pred}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | pat_first, pat_last | a pair of iterators designating the string to be searched for}}
+
{{par|pat_first, pat_last|a pair of iterators designating the string to be searched for}}
{{par | pred | a callable object used to determine equality }}
+
{{par|pred|a callable object used to determine equality}}
 
{{par end}}
 
{{par end}}
  
Line 35: Line 35:
 
}}
 
}}
  
{{member | {{small|std::default_searcher::}}operator() | 2=
+
{{member|{{small|std::default_searcher::}}operator()|2=
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl |  
+
{{dcla|since=c++17|constexpr=c++20|1=
 
template< class ForwardIt2 >
 
template< class ForwardIt2 >
 
std::pair<ForwardIt2, ForwardIt2>
 
std::pair<ForwardIt2, ForwardIt2>
Line 45: Line 45:
 
The member function called by the Searcher overload of {{lc|std::search}} to perform a search with this searcher.
 
The member function called by the Searcher overload of {{lc|std::search}} to perform a search with this searcher.
  
Returns a pair of iterators {{tt|i, j}}, where {{tt|i}} is {{c|std::search(first, last, pat_first, pat_last, pred)}} and {{tt|j}} is {{c|std::next(i, std::distance(pat_first, pat_last))}} unless {{tt|std::search}} returned {{tt|last}} (no match), in which case {{tt|j}} equals {{tt|last}} as well.
+
Returns a pair of iterators {{tt|i, j}}, where {{tt|i}} is {{c|std::search(first, last, pat_first, pat_last, pred)}} and {{tt|j}} is {{c|std::next(i, std::distance(pat_first, pat_last))}} unless {{tt|std::search}} returned {{c|last}} (no match), in which case {{tt|j}} equals {{c|last}} as well.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | first, last | a pair of iterators designating the string to be examined}}
+
{{par|first, last|a pair of iterators designating the string to be examined}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
A pair of iterators to the first and one past last positions in {{math|[first, last)}} where a subsequence that compares equal to {{math|[pat_first, pat_last)}} as defined by {{tt|pred}} is located, or a pair of copies of {{tt|last}} otherwise.
+
A pair of iterators to the first and one past last positions in {{range|first|last}} where a subsequence that compares equal to {{range|pat_first|pat_last}} as defined by {{c|pred}} is located, or a pair of copies of {{c|last}} otherwise.
 
}}
 
}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
|code=
+
#include <iostream>
+
#include <string>
+
 
#include <algorithm>
 
#include <algorithm>
 
#include <functional>
 
#include <functional>
 +
#include <iomanip>
 +
#include <iostream>
 +
#include <string_view>
 
   
 
   
 
int main()
 
int main()
 
{
 
{
     std::string in = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
+
     constexpr std::string_view in =
                    " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
+
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
     std::string needle = "pisci";
+
        "do eiusmod tempor incididunt ut labore et dolore magna aliqua";
 +
 
 +
     const std::string_view needle{"pisci"};
 +
 
 
     auto it = std::search(in.begin(), in.end(),
 
     auto it = std::search(in.begin(), in.end(),
                  std::default_searcher(
+
                  std::default_searcher(
                      needle.begin(), needle.end()));
+
                      needle.begin(), needle.end()));
     if(it != in.end())
+
     if (it != in.end())
         std::cout << "The string " << needle << " found at offset "
+
         std::cout << "The string " << std::quoted(needle) << " found at offset "
 
                   << it - in.begin() << '\n';
 
                   << it - in.begin() << '\n';
 
     else
 
     else
         std::cout << "The string " << needle << " not found\n";
+
         std::cout << "The string " << std::quoted(needle) << " not found\n";
 
}
 
}
 
|output=
 
|output=
The string pisci found at offset 43
+
The string "pisci" found at offset 43
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc search}}
+
{{dsc inc|cpp/algorithm/dsc search}}
 +
{{dsc inc|cpp/utility/functional/dsc boyer_moore_searcher}}
 +
{{dsc inc|cpp/utility/functional/dsc boyer_moore_horspool_searcher}}
 
{{dsc end}}
 
{{dsc end}}
 +
 +
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 21:35, 13 July 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Function objects
Function invocation
(C++17)(C++23)
Identity function object
(C++20)
Transparent operator wrappers
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

Negators
(C++17)
Searchers
default_searcher
(C++17)
Old binders and adaptors
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)  
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)

(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
 
Defined in header <functional>
template< class ForwardIt, class BinaryPredicate = std::equal_to<> >
class default_searcher;
(since C++17)

A class suitable for use with Searcher overload of std::search that delegates the search operation to the pre-C++17 standard library's std::search.

std::default_searcher is CopyConstructible and CopyAssignable.

Contents

[edit] Member functions

std::default_searcher::default_searcher

default_searcher( ForwardIt pat_first,

                  ForwardIt pat_last,

                  BinaryPredicate pred = BinaryPredicate() );
(since C++17)
(constexpr since C++20)

Constructs a std::default_searcher by storing copies of pat_first, pat_last, and pred.

Parameters

pat_first, pat_last - a pair of iterators designating the string to be searched for
pred - a callable object used to determine equality

Exceptions

Any exceptions thrown by the copy constructors of BinaryPredicate or ForwardIt.

std::default_searcher::operator()

template< class ForwardIt2 >

std::pair<ForwardIt2, ForwardIt2>

    operator()( ForwardIt2 first, ForwardIt2 last ) const;
(since C++17)
(constexpr since C++20)

The member function called by the Searcher overload of std::search to perform a search with this searcher.

Returns a pair of iterators i, j, where i is std::search(first, last, pat_first, pat_last, pred) and j is std::next(i, std::distance(pat_first, pat_last)) unless std::search returned last (no match), in which case j equals last as well.

Parameters

first, last - a pair of iterators designating the string to be examined

Return value

A pair of iterators to the first and one past last positions in [firstlast) where a subsequence that compares equal to [pat_firstpat_last) as defined by pred is located, or a pair of copies of last otherwise.

[edit] Example

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string_view>
 
int main()
{
    constexpr std::string_view in =
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
        "do eiusmod tempor incididunt ut labore et dolore magna aliqua";
 
    const std::string_view needle{"pisci"};
 
    auto it = std::search(in.begin(), in.end(),
                  std::default_searcher(
                      needle.begin(), needle.end()));
    if (it != in.end())
        std::cout << "The string " << std::quoted(needle) << " found at offset "
                  << it - in.begin() << '\n';
    else
        std::cout << "The string " << std::quoted(needle) << " not found\n";
}

Output:

The string "pisci" found at offset 43

[edit] See also

searches for the first occurrence of a range of elements
(function template) [edit]
Boyer-Moore search algorithm implementation
(class template) [edit]
Boyer-Moore-Horspool search algorithm implementation
(class template) [edit]