Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | utility‎ | functional
(rename per usual style)
m (fix title; fix example)
Line 1: Line 1:
{{cpp/title|default_searcher|make_default_searcher}}
+
{{cpp/title|default_searcher}}
 
{{cpp/utility/functional/navbar}}
 
{{cpp/utility/functional/navbar}}
  
Line 71: Line 71:
 
     std::string needle = "pisci";
 
     std::string needle = "pisci";
 
     auto it = std::search(in.begin(), in.end(),
 
     auto it = std::search(in.begin(), in.end(),
                   std::make_default_searcher(
+
                   std::default_searcher(
                       needle.begin(), needle.end()));
+
                       needle.begin(), needle.end())).first;
 
     if(it != in.end())
 
     if(it != in.end())
 
         std::cout << "The string " << needle << " found at offset "
 
         std::cout << "The string " << needle << " found at offset "

Revision as of 09:28, 11 March 2017

 
 
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 Template:concept overload of std::search that delegates the search operation to the pre-C++17 standard library's std::search.

default_searcher is Template:concept and Template:concept.

Contents

Member functions

std::default_searcher::default_searcher

default_searcher( ForwardIt pat_first,

                  ForwardIt pat_last,

                  BinaryPredicate pred = BinaryPredicate());

Constructs a 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;

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 [first, last) where a subsequence that compares equal to [pat_first, pat_last) as defined by pred is located, or a pair of copies of last otherwise.

Example

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

Output:

The string pisci found at offset 43

See also

searches for the first occurrence of a range of elements
(function template) [edit]