名稱空間
變體
操作

std::find_end

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <algorithm>
template< class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,

                     ForwardIt2 s_first, ForwardIt2 s_last );
(1) (C++20 起為 constexpr)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_end( ExecutionPolicy&& policy,
                     ForwardIt1 first, ForwardIt1 last,

                     ForwardIt2 s_first, ForwardIt2 s_last );
(2) (C++17 起)
template< class ForwardIt1, class ForwardIt2, class BinaryPred >

ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
                     ForwardIt2 s_first, ForwardIt2 s_last,

                     BinaryPred p );
(3) (C++20 起為 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_end( ExecutionPolicy&& policy,
                     ForwardIt1 first, ForwardIt1 last,
                     ForwardIt2 s_first, ForwardIt2 s_last,

                     BinaryPred p );
(4) (C++17 起)

在範圍 [firstlast) 中搜索序列 [s_firsts_last) 的最後一次出現。

1) 使用 operator== 比較元素。
3) 使用給定二元謂詞 p 比較元素。
2,4)(1,3),但按 policy 執行。
僅當滿足所有以下條件時,這些過載才參與過載決議

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true

(C++20 起)

目錄

[編輯] 引數

first, last - 定義要檢查的元素範圍的迭代器對
s_first, s_last - 定義要搜尋元素的範圍的迭代器對
policy - 要使用的 執行策略
p - 二元謂詞,如果元素應被視為相等,則返回 true。

謂詞函式的簽名應等效於以下內容:

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

雖然簽名不需要包含 const &,但函式不得修改傳遞給它的物件,並且必須能夠接受型別為 (可能為 const) Type1Type2 的所有值,而不管 值類別 (因此,不允許使用 Type1 &,也不允許使用 Type1,除非對於 Type1 移動等同於複製(C++11 起))。
型別 Type1Type2 必須使得型別 ForwardIt1ForwardIt2 的物件可以被解引用,然後隱式轉換為 Type1Type2

型別要求
-
ForwardIt1 必須滿足 LegacyForwardIterator 的要求。
-
ForwardIt2 必須滿足 LegacyForwardIterator 的要求。

[編輯] 返回值

指向範圍 [firstlast) 中序列 [s_firsts_last) 最後一次出現的開頭的迭代器。

如果 [s_firsts_last) 為空,或者沒有找到這樣的序列,則返回 last

[編輯] 複雜度

給定 N 作為 std::distance(first, last),以及 S 作為 std::distance(s_first, s_last)

1,2) 使用 operator== 最多進行 S·(N-S+1) 次比較。
3,4) 最多進行 S·(N-S+1) 次謂詞 p 的應用。

[編輯] 異常

帶有模板引數 ExecutionPolicy 的過載按如下方式報告錯誤

  • 如果作為演算法一部分呼叫的函式丟擲異常,並且 ExecutionPolicy標準策略 之一,則呼叫 std::terminate。對於任何其他 ExecutionPolicy,行為是實現定義的。
  • 如果演算法未能分配記憶體,則丟擲 std::bad_alloc

[編輯] 可能的實現

find_end (1)
template<class ForwardIt1, class ForwardIt2>
constexpr //< since C++20
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 s_first, ForwardIt2 s_last)
{
    if (s_first == s_last)
        return last;
 
    ForwardIt1 result = last;
    while (true)
    {
        ForwardIt1 new_result = std::search(first, last, s_first, s_last);
        if (new_result == last)
            break;
        else
        {
            result = new_result;
            first = result;
            ++first;
        }
    }
    return result;
}
find_end (3)
template<class ForwardIt1, class ForwardIt2, class BinaryPred>
constexpr //< since C++20
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 s_first, ForwardIt2 s_last,
                    BinaryPred p)
{
    if (s_first == s_last)
        return last;
 
    ForwardIt1 result = last;
    while (true)
    {
        ForwardIt1 new_result = std::search(first, last, s_first, s_last, p);
        if (new_result == last)
            break;
        else
        {
            result = new_result;
            first = result;
            ++first;
        }
    }
    return result;
}

[編輯] 示例

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
 
auto print_result = [](auto result, const auto& v)
{
    result == v.end()
        ? std::cout << "Sequence not found\n"
        : std::cout << "Last occurrence is at: " << std::distance(v.begin(), result)
                    << '\n';
};
 
int main()
{
    const auto v = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
 
    for (auto const& x : {std::array{1, 2, 3}, {4, 5, 6}})
    {
        auto iter = std::find_end(v.begin(), v.end(), x.begin(), x.end()); // overload (1)
        print_result(iter, v);
    }
 
    for (auto const& x : {std::array{-1, -2, -3}, {-4, -5, -6}})
    {
        auto iter = std::find_end(v.begin(), v.end(), x.begin(), x.end(), // overload (3)
                                  [](int x, int y)
                                  {
                                      return std::abs(x) == std::abs(y);
                                  });
        print_result(iter, v);
    }
}

輸出

Last occurrence is at: 8
Sequence not found
Last occurrence is at: 8
Sequence not found

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 1205 C++98 如果 [s_firsts_last) 為空,返回值不明確。 在這種情況下返回 last
LWG 2150 C++98 “序列出現”的條件不正確 已更正

[編輯] 參閱

搜尋一個範圍的元素首次出現的位置
(函式模板) [編輯]
如果一個序列是另一個序列的子序列,則返回 true
(函式模板) [編輯]
尋找第一對相等的(或滿足給定謂詞的)相鄰項
(函式模板) [編輯]
尋找第一個滿足特定條件的元素
(函式模板) [編輯]
搜尋一組元素中的任何一個
(函式模板) [編輯]
在一個範圍內搜尋一個元素的連續 N 次副本首次出現的位置
(函式模板) [編輯]
在特定範圍中尋找最後一次出現的元素序列
(演算法函式物件)[編輯]