std::make_move_iterator
來自 cppreference.com
< cpp | 迭代器 (iterator)
定義於標頭檔案 <iterator> |
||
template< class Iter > std::move_iterator<Iter> make_move_iterator( Iter i ); |
(C++11 起) (自 C++17 起為 constexpr) |
|
make_move_iterator
是一個便捷函式模板,用於為給定迭代器 i 構造 std::move_iterator,其型別從引數型別推導而來。
目錄 |
[edit] 引數
i | - | 要轉換為移動迭代器的輸入迭代器 |
[edit] 返回值
std::move_iterator<Iter>(std::move(i))
[edit] 示例
執行此程式碼
#include <iomanip> #include <iostream> #include <iterator> #include <list> #include <string> #include <vector> auto print = [](const auto rem, const auto& seq) { for (std::cout << rem; const auto& str : seq) std::cout << std::quoted(str) << ' '; std::cout << '\n'; }; int main() { std::list<std::string> s{"one", "two", "three"}; std::vector<std::string> v1(s.begin(), s.end()); // copy std::vector<std::string> v2(std::make_move_iterator(s.begin()), std::make_move_iterator(s.end())); // move print("v1 now holds: ", v1); print("v2 now holds: ", v2); print("original list now holds: ", s); }
可能的輸出
v1 now holds: "one" "two" "three" v2 now holds: "one" "two" "three" original list now holds: "" "" ""
[edit] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2061 | C++11 | make_move_iterator 未將陣列引數轉換為指標 |
已轉換為可轉換 |
[edit] 參閱
(C++11) |
解引用為右值的迭代器介面卡 (類模板) |
(C++11) |
將引數轉換為亡值 (函式模板) |