首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::search

Defined in header <algorithm>

?

?

template< class ForwardIt1, class ForwardIt2 > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last );

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt1 search( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last );

(2)

(since C++17)

template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );

(3)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 search( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );

(4)

(since C++17)

template<class ForwardIterator, class Searcher> ForwardIterator search( ForwardIterator first, ForwardIterator last, const Searcher& searcher );

(5)

(since C++17)

1-4%29搜索元素子序列的第一次出现。[s_first, s_last)在范围内[first, last - (s_last - s_first))...

1%29个元素的比较operator==...

使用给定的二进制谓词对3%29个元素进行比较。p...

2,4%29与%281,3%29相同,但根据policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

5%29搜索序列。[的构造函数中指定的模式的最后%29。searcher.有效地执行return searcher(first, last).first;...Searcher不必CopyConstructible...

The standard library provides the following searchers: default_searcher (C++17) standard C++ library search algorithm implementation (class template) boyer_moore_searcher (C++17) Boyer-Moore search algorithm implementation (class template) boyer_moore_horspool_searcher (C++17) Boyer-Moore-Horspool search algorithm implementation (class template)

default_searcher (C++17)

standard C++ library search algorithm implementation (class template)

boyer_moore_searcher (C++17)

Boyer-Moore search algorithm implementation (class template)

boyer_moore_horspool_searcher (C++17)

Boyer-Moore-Horspool search algorithm implementation (class template)

(since C++17)

default_searcher (C++17)

standard C++ library search algorithm implementation (class template)

boyer_moore_searcher (C++17)

Boyer-Moore search algorithm implementation (class template)

boyer_moore_horspool_searcher (C++17)

Boyer-Moore-Horspool search algorithm implementation (class template)

参数

first, last

-

the range of elements to examine

s_first, s_last

-

the range of elements to search for

policy

-

the execution policy to use. See execution policy for details.

searcher

-

the searcher encapsulating the search algorithm and the pattern to look for

p

-

binary predicate which returns ?true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function must not modify the objects passed to it. The types Type1 and Type2 must be such that objects of types ForwardIt1 and ForwardIt2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively. ?

类型要求

-前进1,前进2必须符合先行者的要求。

-搜索者必须符合搜索者的要求。

返回值

1-4%29位爱尔兰人开始第一个子序列[s_first, s_last)在范围内[first, last - (s_last - s_first))如果没有找到这样的子序列,last会被归还。

如果[s_first, s_last)是空的,first会被归还。%28自C++11%29

5%29返回searcher.operator(),即,到找到子字符串的位置的迭代器或last如果它没被找到的话。

复杂性

1-4%29S*N比较S =std::distance(s_first, s_last)N =std::distance(first, last)...

5%29取决于搜索者

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<类ForwardIt 1,类ForwardIt 2>ForwardIt 1搜索%28ForwardIt1,ForwardIt 1最后,ForwardIt 2 s[医]第一,前进2[医]最后%29(表示%28;;++First%29){ForwardIt 1 it=First;%28 ForwardIt 2 s[医]它...[医]首先;;;++it,++s[医]它%29(如果%28 s)[医]它=[医]最后%29{先返回;}如果%28 it=最后%29{返回最后;}如果%28%21%28%2A它==%2A斯[医]它%29%29{Break;}}}

第二版

模板<类ForwardIt 1,类ForwardIt 2,类BinaryPredicate>ForwardIt 1搜索%28ForwardIt1,ForwardIt 1,ForwardIt 2[医]第一,前进2[医]最后,BinaryPredicate p%29{表示%28;++First%29{ForwardIt 1 it=First;对于%28 ForwardIt 2 s[医]它...[医]首先;;;++it,++s[医]它%29(如果%28 s)[医]它=[医]最后%29{先返回;}如果%28 it=最后%29{返回最后;}如果%28%21 p%28%2A它,%2A斯[医]它%29%29{Break;}}}

二次

代码语言:javascript
复制
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
 
template<typename Container>
bool in_quote(const Container& cont, const std::string& s)
{
    return std::search(cont.begin(), cont.end(), s.begin(), s.end()) != cont.end();
}
 
int main()
{
    std::string str = "why waste time learning, when ignorance is instantaneous?";
    // str.find() can be used as well
    std::cout << std::boolalpha << in_quote(str, "learning") << '\n'
                                << in_quote(str, "lemming")  << '\n';
 
    std::vector<char> vec(str.begin(), str.end());
    std::cout << std::boolalpha << in_quote(vec, "learning") << '\n'
                                << in_quote(vec, "lemming")  << '\n';
 
    // The C++17 overload demo:
    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::make_boyer_moore_searcher(
                       needle.begin(), needle.end()));
    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";
}

二次

产出:

二次

代码语言:javascript
复制
true
false
true
false
The string pisci found at offset 43

二次

另见

find_end

finds the last sequence of elements in a certain range (function template)

includes

returns true if one set is a subset of another (function template)

equal

determines if two sets of elements are the same (function template)

findfind_iffind_if_not (C++11)

finds the first element satisfying specific criteria (function template)

lexicographical_compare

returns true if one range is lexicographically less than another (function template)

mismatch

finds the first position where two ranges differ (function template)

search_n

searches for a number consecutive copies of an element in a range (function template)

default_searcher (C++17)

standard C++ library search algorithm implementation (class template)

boyer_moore_searcher (C++17)

Boyer-Moore search algorithm implementation (class template)

boyer_moore_horspool_searcher (C++17)

Boyer-Moore-Horspool search algorithm implementation (class template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com