std::ranges::move_backward, std::ranges::move_backward_result
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::bidirectional_iterator I1, std::sentinel_for<I1> S1, std::bidirectional_iterator I2 > |
(1) | (C++20 起) |
template< ranges::bidirectional_range R, std::bidirectional_iterator I > requires std::indirectly_movable<ranges::iterator_t<R>, I> |
(2) | (C++20 起) |
輔助型別 |
||
template< class I, class O > using move_backward_result = ranges::in_out_result<I, O>; |
(3) | (C++20 起) |
1) 將由
[
first,
last)
定義的範圍內的元素移動到另一個範圍 [
d_last - N,
d_last)
,其中 N = ranges::distance(first, last)。元素以逆序移動(最後一個元素最先移動),但它們的相對順序被保留。如果 d_last 在 (first, last]
範圍內,則行為未定義。在這種情況下,可以使用 ranges::move 代替。已移動的範圍中的元素仍將包含相應型別的有效值,但不一定是移動之前的值,如同對每個整數 n
(其中 0 ≤ n < N)使用 *(d_last - n) = ranges::iter_move(last - n)。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first, last | - | 定義要移動元素的範圍的迭代器-哨兵對 |
r | - | 要移動元素的範圍 |
d_last | - | 目標範圍的末尾 |
[編輯] 返回值
{last, d_last - N}.
[編輯] 複雜度
1) 恰好 N 次移動賦值。
2) 恰好 ranges::distance(r) 次移動賦值。
[編輯] 注意
當移動重疊範圍時,當向左移動(目標範圍的起始點在源範圍之外)時,ranges::move 是合適的;當向右移動(目標範圍的結束點在源範圍之外)時,ranges::move_backward 是合適的。
[編輯] 可能實現
struct move_backward_fn { template<std::bidirectional_iterator I1, std::sentinel_for<I1> S1, std::bidirectional_iterator I2> requires std::indirectly_movable<I1, I2> constexpr ranges::move_backward_result<I1, I2> operator()(I1 first, S1 last, I2 d_last) const { auto i {last}; for (; i != first; *--d_last = ranges::iter_move(--i)) {} return {std::move(last), std::move(d_last)}; } template<ranges::bidirectional_range R, std::bidirectional_iterator I> requires std::indirectly_movable<ranges::iterator_t<R>, I> constexpr ranges::move_backward_result<ranges::borrowed_iterator_t<R>, I> operator()(R&& r, I d_last) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(d_last)); } }; inline constexpr move_backward_fn move_backward {}; |
[編輯] 示例
執行此程式碼
#include <algorithm> #include <iostream> #include <string> #include <string_view> #include <vector> using Vec = std::vector<std::string>; void print(std::string_view rem, Vec const& vec) { std::cout << rem << "[" << vec.size() << "]: "; for (const std::string& s : vec) std::cout << (s.size() ? s : std::string{"·"}) << ' '; std::cout << '\n'; } int main() { Vec a{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"}; Vec b(a.size()); print("Before move:\n" "a", a); print("b", b); std::ranges::move_backward(a, b.end()); print("\n" "Move a >> b:\n" "a", a); print("b", b); std::ranges::move_backward(b.begin(), b.end(), a.end()); print("\n" "Move b >> a:\n" "a", a); print("b", b); std::ranges::move_backward(a.begin(), a.begin()+3, a.end()); print("\n" "Overlapping move a[0, 3) >> a[5, 8):\n" "a", a); }
可能的輸出
Before move: a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ b[8]: · · · · · · · · Move a >> b: a[8]: · · · · · · · · b[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ Move b >> a: a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ b[8]: · · · · · · · · Overlapping move a[0, 3) >> a[5, 8): a[8]: · · · ▄ ▅ ▁ ▂ ▃
[編輯] 參閱
(C++20) |
將一個範圍的元素移動到一個新位置 (演算法函式物件) |
(C++20)(C++20) |
將一個範圍的元素複製到一個新位置 (演算法函式物件) |
(C++20) |
以逆序複製一個範圍的元素 (演算法函式物件) |
(C++11) |
將一個範圍的元素移動到一個新位置 (函式模板) |
(C++11) |
將引數轉換為亡值 (函式模板) |