std::ranges::mismatch, std::ranges::mismatch_result
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, |
(1) | (C++20 起) |
template< ranges::input_range R1, ranges::input_range R2, class Pred = ranges::equal_to, |
(2) | (C++20 起) |
輔助型別 |
||
template< class I1, class I2 > using mismatch_result = ranges::in_in_result<I1, I2>; |
(3) | (C++20 起) |
返回兩個範圍中第一個不匹配的投影元素對:一個由 [
first1,
last1)
或 r1 定義,另一個由 [
first2,
last2)
或 r2 定義。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first1, last1 | - | 定義要比較的第一個元素範圍的迭代器-哨兵對 |
r1 | - | 要比較的第一個元素範圍 |
first2, last2 | - | 定義要比較的第二個元素範圍的迭代器-哨兵對 |
r2 | - | 要比較的第二個元素範圍 |
pred | - | 應用於投影元素的謂詞 |
proj1 | - | 應用於第一個元素範圍的投影 |
proj2 | - | 應用於第二個元素範圍的投影 |
[編輯] 返回值
ranges::mismatch_result
,包含指向前兩個不相等元素的迭代器。
如果在比較達到 last1 或 last2(先達到的那個)時沒有找到不匹配項,則該物件包含結束迭代器和來自另一個範圍的相應迭代器。
[編輯] 複雜度
最多 std::min(last1 - first1, last2 - first2) 次謂詞和相應投影的應用。
[編輯] 可能的實現
struct mismatch_fn { template<std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, class Pred = ranges::equal_to, class Proj1 = std::identity, class Proj2 = std::identity> requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2> constexpr std::mismatch_result<I1, I2> operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { for (; first1 != last1 && first2 != last2; ++first1, (void)++first2) if (not std::invoke(pred, std::invoke(proj1, *first1), std::invoke(proj2, *first2))) break; return {first1, first2}; } template<ranges::input_range R1, ranges::input_range R2, class Pred = ranges::equal_to, class Proj1 = std::identity, class Proj2 = std::identity> requires std::indirectly_comparable<ranges::iterator_t<R1>, ranges::iterator_t<R2>, Pred, Proj1, Proj2> constexpr ranges::mismatch_result<ranges::borrowed_iterator_t<R1>, ranges::borrowed_iterator_t<R2>> operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), std::ref(pred), std::ref(proj1), std::ref(proj2)); } }; inline constexpr mismatch_fn mismatch; |
[編輯] 示例
此程式確定給定字串開頭和結尾處同時找到的最長子字串,按相反順序(可能重疊)。
#include <algorithm> #include <iostream> #include <ranges> #include <string_view> [[nodiscard]] constexpr std::string_view mirror_ends(const std::string_view in) { const auto end = std::ranges::mismatch(in, in | std::views::reverse).in1; return {in.cbegin(), end}; } int main() { std::cout << mirror_ends("abXYZba") << '\n' << mirror_ends("abca") << '\n' << mirror_ends("ABBA") << '\n' << mirror_ends("level") << '\n'; using namespace std::literals::string_view_literals; static_assert("123"sv == mirror_ends("123!@#321")); static_assert("radar"sv == mirror_ends("radar")); }
輸出
ab a ABBA level
[編輯] 參閱
(C++20) |
判斷兩組元素是否相同 (演算法函式物件) |
(C++20)(C++20)(C++20) |
尋找第一個滿足特定條件的元素 (演算法函式物件) |
如果一個範圍在字典上小於另一個範圍,則返回 true (演算法函式物件) | |
(C++20) |
搜尋一個範圍的元素首次出現的位置 (演算法函式物件) |
尋找兩個範圍開始不同的第一個位置 (函式模板) |