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

std::find_end cppreference.com
cppreference.com

std::find_end

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 find_end( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last );
(1)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );
(2)

[s_first, s_last) [first, last). 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_first, s_last) [first, last).

, last. ( C++11)

[s_first, s_last) , , last. ( C++11)

S*(N-S+1) , S = distance(s_first, s_last), N = distance(first, last).

template<class ForwardIt1, class ForwardIt2>
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 s_first, ForwardIt2 s_last)
{
    if (s_first == s_last)
        return last;
    ForwardIt1 result = last;
    while (1) {
        ForwardIt1 new_result = std::search(first, last, s_first, s_last);
        if (new_result == last) {
            return result;
        } else {
            result = new_result;
            first = result;
            ++first;
        }
    }
    return result;
}
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 s_first, ForwardIt2 s_last,
                    BinaryPredicate p)
{
    if (s_first == s_last)
        return last;
    ForwardIt1 result = last;
    while (1) {
        ForwardIt1 new_result = std::search(first, last, s_first, s_last, p);
        if (new_result == last) {
            return result;
        } else {
            result = new_result;
            first = result;
            ++first;
        }
    }
    return result;
}

find_end() .

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
    std::vector<int>::iterator result;

    std::vector<int> t1{1, 2, 3};

    result = std::find_end(v.begin(), v.end(), t1.begin(), t1.end());
    if (result == v.end()) {
        std::cout << "  \n";
    } else {
        std::cout << "   : "
                  << std::distance(v.begin(), result) << "\n";
    }

    std::vector<int> t2{4, 5, 6};
    result = std::find_end(v.begin(), v.end(), t2.begin(), t2.end());
    if (result == v.end()) {
        std::cout << "  \n";
    } else {
        std::cout << "   : "
                  << std::distance(v.begin(), result) << "\n";
    }
}

:

   : 8
  

.

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

( ) []

( ) []

Web Proxy Viewer  |  New URL  |  Original Page