std::ranges::ends_with
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, |
(1) | (C++23 起) |
template< ranges::input_range R1, ranges::input_range R2, class Pred = ranges::equal_to, |
(2) | (C++23 起) |
檢查第二個範圍是否與第一個範圍的字尾匹配。
- 若 N1 < N2 為 true,則返回 false。
- 否則,返回 ranges::equal(std::move(first1) + (N1 - N2), last1,
std::move(first2), last2, pred, proj1, proj2)。
- 若 N1 < N2 為 true,則返回 false。
- 否則,返回 ranges::equal(views::drop(ranges::ref_view(r1),
N1 - static_cast<decltype(N1)>(N2)),
r2, pred, proj1, proj2)。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first1, last1 | - | 定義要檢查的元素 範圍 的迭代器-哨兵對 |
r1 | - | 要檢查的元素範圍 |
first2, last2 | - | 定義用作字尾的元素範圍的迭代器-哨兵對 |
r2 | - | 用作字尾的元素範圍 |
pred | - | 比較投影元素的二元謂詞 |
proj1 | - | 應用於要檢查的範圍元素的投影 |
proj2 | - | 應用於用作字尾的範圍元素的投影 |
[編輯] 返回值
若第二個範圍與第一個範圍的字尾匹配,則為 true,否則為 false。
[編輯] 複雜度
一般為線性複雜度:最多對謂詞和兩個投影應用 min(N1,N2) 次。若 N1 < N2 為 true,則不應用謂詞和兩個投影。
若 N1 和 N2 都可以在常數時間內計算(即兩個迭代器-哨兵型別對都滿足 sized_sentinel_for
,或兩個範圍型別都滿足 sized_range
),且 N1 < N2 為 true,則時間複雜度為常數。
[編輯] 可能實現
struct ends_with_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::forward_iterator<I1> || std::sized_sentinel_for<S1, I1>) && (std::forward_iterator<I2> || std::sized_sentinel_for<S2, I2>) && std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2> constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { const auto n1 = ranges::distance(first1, last1); const auto n2 = ranges::distance(first2, last2); if (n1 < n2) return false; ranges::advance(first1, n1 - n2); return ranges::equal(std::move(first1), last1, std::move(first2), last2, pred, proj1, proj2); } template<ranges::input_range R1, ranges::input_range R2, class Pred = ranges::equal_to, class Proj1 = std::identity, class Proj2 = std::identity> requires (ranges::forward_range<R1> || ranges::sized_range<R1>) && (ranges::forward_range<R2> || ranges::sized_range<R2>) && std::indirectly_comparable<ranges::iterator_t<R1>, ranges::iterator_t<R2>, Pred, Proj1, Proj2> constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { const auto n1 = ranges::distance(r1); const auto n2 = ranges::distance(r2); if (n1 < n2) return false; return ranges::equal(views::drop(ranges::ref_view(r1), n1 - static_cast<decltype(n1)>(n2)), r2, pred, proj1, proj2); } }; inline constexpr ends_with_fn ends_with{}; |
[編輯] 註解
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_ranges_starts_ends_with |
202106L |
(C++23) | std::ranges::starts_with, std::ranges::ends_with |
[編輯] 示例
#include <algorithm> #include <array> static_assert ( ! std::ranges::ends_with("for", "cast") && std::ranges::ends_with("dynamic_cast", "cast") && ! std::ranges::ends_with("as_const", "cast") && std::ranges::ends_with("bit_cast", "cast") && ! std::ranges::ends_with("to_underlying", "cast") && std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{3, 4}) && ! std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{4, 5}) ); int main() {}
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 4105 | C++23 | 過載 (2) 計算大小 差值透過 N1 - N2[1] |
更改為 N1 - static_cast<decltype(N1)>(N2) |
[編輯] 參閱
(C++23) |
檢查一個範圍是否以另一個範圍開始 (演算法函式物件) |
(C++20) |
檢查字串是否以給定字尾結尾 ( std::basic_string<CharT,Traits,Allocator> 的公開成員函式) |
(C++20) |
檢查字串檢視是否以給定字尾結尾 ( std::basic_string_view<CharT,Traits> 的公開成員函式) |