std::move_iterator
| ヘッダ <iterator> で定義
|
||
template< class Iter > class move_iterator; |
(C++11以上) | |
std::move_iterator は、逆参照がベースとなるイテレータから返された値を右辺値に変換することを除いて、ベースとなるイテレータ (少なくとも LegacyInputIterator でなければなりません) とまったく同様に振る舞うイテレータアダプタです。 このイテレータが入力イテレータとして使用された場合、その効果は値をコピーするのではなくムーブします。
メンバ関数
| メンバ型 | 定義 | ||||||
iterator_type
|
Iter
| ||||||
iterator_category
|
| ||||||
iterator_concept(C++20以上) |
std::input_iterator_tag | ||||||
value_type
|
| ||||||
difference_type
|
| ||||||
pointer
|
Iter
| ||||||
reference
|
|
メンバ関数
| 新しいイテレータアダプタを構築します (パブリックメンバ関数) | |
| 別のイテレータを代入します (パブリックメンバ関数) | |
| ベースとなるイテレータにアクセスします (パブリックメンバ関数) | |
(C++20で非推奨) |
イテレータの指す先の要素にアクセスします (パブリックメンバ関数) |
| インデックスによって要素にアクセスします (パブリックメンバ関数) | |
| イテレータをインクリメントもしくはデクリメントします (パブリックメンバ関数) |
メンバオブジェクト
| メンバ名 | 定義 |
current (private)
|
base() イテレータのコピー (説明専用) |
非メンバ関数
| ベースとなるイテレータを比較します (関数テンプレート) | |
| ベースとなるイテレータとベースとなる番兵を比較します (関数テンプレート) | |
| イテレータを進めます (関数テンプレート) | |
| 2つのイテレータアダプタ間の距離を計算します (関数テンプレート) | |
| ベースとなるイテレータとベースとなる番兵の間の距離を計算します (関数テンプレート) | |
(C++20) |
ベースとなるイテレータを逆参照した結果をその関連する右辺値参照型にキャストします (関数テンプレート) |
(C++20) |
2つのベースとなるイテレータの指す先のオブジェクトを入れ替えます (関数テンプレート) |
例
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <numeric>
#include <string>
int main()
{
std::vector<std::string> v{"this", "is", "an", "example"};
std::cout << "Old contents of the vector: ";
for (auto& s : v)
std::cout << '"' << s << "\" ";
typedef std::vector<std::string>::iterator iter_t;
std::string concat = std::accumulate(
std::move_iterator<iter_t>(v.begin()),
std::move_iterator<iter_t>(v.end()),
std::string()); // std::make_move_iterator を使えばもっと簡単にできます
std::cout << "\nConcatenated as string: " << concat << '\n'
<< "New contents of the vector: ";
for (auto& s : v)
std::cout << '"' << s << "\" ";
std::cout << '\n';
}
出力例:
Old contents of the vector: "this" "is" "an" "example"
Concatenated as string: thisisanexample
New contents of the vector: "" "" "" ""
関連項目
(C++11) |
引数から推定した型の std::move_iterator を作成します (関数テンプレート) |
(C++20) |
std::move_iterator と共に使用するための番兵アダプタ (クラステンプレート) |