std::rotate_copy
來自 cppreference.com
定義於標頭檔案 <algorithm> |
||
template< class ForwardIt, class OutputIt > OutputIt rotate_copy( ForwardIt first, ForwardIt middle, |
(1) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > |
(2) | (C++17 起) |
將 [
first,
last)
的左旋轉複製到 d_first。
1) 複製範圍
[
first,
last)
中的元素,使得在以 d_first 開頭的目標範圍中,[
first,
middle)
中的元素被放置在 [
middle,
last)
中的元素之後,同時兩個範圍中元素的順序都得以保留。2) 同 (1),但根據 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,
middle)
或[
middle,
last)
不是 有效範圍。 - 源範圍和目標範圍重疊。
目錄 |
[編輯] 引數
first, last | - | 定義要複製的元素的源 範圍 的迭代器對 |
middle | - | 指向 [ first, last) 中一個元素的迭代器,該元素應出現在新範圍的開頭 |
d_first | - | 目標範圍的開頭 |
policy | - | 要使用的 執行策略 |
型別要求 | ||
-ForwardIt, ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。 | ||
-OutputIt 必須滿足 LegacyOutputIterator 的要求。 |
[編輯] 返回值
指向複製的最後一個元素之後元素的輸出迭代器。
[編輯] 複雜度
恰好 std::distance(first, last) 次賦值。
[編輯] 異常
帶有名為 ExecutionPolicy
的模板引數的過載會按如下方式報告錯誤:
- 如果作為演算法一部分呼叫的函式執行丟擲異常且
ExecutionPolicy
是 標準策略 之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為是實現定義的。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 可能的實現
另請參閱 libstdc++、libc++ 和 MSVC STL 中的實現。
[編輯] 示例
執行此程式碼
#include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> src{1, 2, 3, 4, 5}; std::vector<int> dest(src.size()); auto pivot = std::find(src.begin(), src.end(), 3); std::rotate_copy(src.begin(), pivot, src.end(), dest.begin()); for (int i : dest) std::cout << i << ' '; std::cout << '\n'; // copy the rotation result directly to the std::cout pivot = std::find(dest.begin(), dest.end(), 1); std::rotate_copy(dest.begin(), pivot, dest.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; }
輸出
3 4 5 1 2 1 2 3 4 5
[編輯] 參閱
旋轉一個範圍中元素的順序 (函式模板) | |
(C++20) |
複製並旋轉一個範圍的元素 (演算法函式物件) |