[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/algorithm/search [Back]  [Original]

std::search cppreference.com
cppreference.com

std::search

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
(C++20)
, ranges::copy, ranges::sort, ...
(C++17)
(C++11)(C++11)(C++11)
(C++17)
(C++11)
( )
(C++11)
/
(C++11)
(C++17)

(C++17)
(C++17)
(C++17)
C
 
<tbody> </tbody>
template< class ForwardIt1, class ForwardIt2 > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last );
(1)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );
(2)

[s_firsts_last) [firstlast - (s_last - s_first)). operator== , p.

[firstlast)
[s_firsts_last)
p , true .

:

bool pred(const Type1 &a, const Type2 &b);

const &, .
Type1 Type2 , ForwardIt1 ForwardIt2 Type1 Type2 .

-
ForwardIt1 ForwardIterator.
-
ForwardIt2 ForwardIterator.

[s_firsts_last) [firstlast - (s_last - s_first)). , last.

[s_firsts_last) , first. ( C++11)

S*N , S= std::distance(s_first, s_last), N = std::distance(first, last).

template<class ForwardIt1, class ForwardIt2>
ForwardIt1 search(ForwardIt1 first, ForwardIt1 last,
                        ForwardIt2 s_first, ForwardIt2 s_last)
{
    for (; ; ++first) {
        ForwardIt1 it = first;
        for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) {
            if (s_it == s_last) {
                return first;
            }
            if (it == last) {
                return last;
            }
            if (!(*it == *s_it)) {
                break;
            }
        }
    }
}
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
ForwardIt1 search(ForwardIt1 first, ForwardIt1 last,
                        ForwardIt2 s_first, ForwardIt2 s_last,
                        BinaryPredicate p)
{
    for (; ; ++first) {
        ForwardIt1 it = first;
        for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) {
            if (s_it == s_last) {
                return first;
            }
            if (it == last) {
                return last;
            }
            if (!p(*it, *s_it)) {
                break;
            }
        }
    }
}

#include <string>
#include <algorithm>
#include <iostream>

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 = "    ,    ?";
    //    str.find()
    std::cout << std::boolalpha << in_quote(str, "") << '\n'
                                << in_quote(str, "")  << '\n';

    std::vector<char> vec(str.begin(), str.end());
    std::cout << std::boolalpha << in_quote(vec, "") << '\n'
                                << in_quote(vec, "")  << '\n';
}

:

true
false
true
false

.


( ) []
,
( ) []
,
( ) []
true,
( ) []
,
( ) []

( ) []

Web Proxy Viewer  |  New URL  |  Original Page