Overview
move_iterator behaves like a normal iterator except dereferenced element converted into a rvalue as if move() is called on it.
Details
A copy of the original iterator (the base iterator) is kept internally and used to reflect the operations performed on the move_iterator. A copy of the base iterator with the current state can be obtained at any time by calling member base.
Syntax
The syntax is as below. Template parameter Iter represents the original iterator for which move_iterator is created. Iter can be bidirectional iterator or a random-access iterator.
template < class Iter >class reverse iterator;
Members
It defines following types.
member type | definition |
---|---|
iterator_type | Iter |
iterator_category | iterator_traits<Iter>::iterator_category |
value_type | iterator_traits<Iter>::value_type |
difference_type | iterator_traits<Iter>::difference_type |
pointer | iterator_traits<Iter>::pointer |
reference | value_type&& |
Operation
A move_iterator behaves like a regular iterator except dereferencing converts the element to a rvalue.
This can be clearly seen in the example below.
vector<string> v{"one","two","three"},v2;//same as
//for_each(begin(v),end(v),[&v2](string& s){back_inserter(v2) = move(s);});move_iterator<vector<string>::iterator> itrbeg(v.begin()); move_iterator<vector<string>::iterator> itrend(v.end());//v:{"","",""}
//v2:{"one","two","three"}
copy(itrbeg, itrend, back_inserter(v2));
Functionality
Constructors
Name | Description |
---|---|
move_iterator() | Default constructor Example: vector<string> v{"one", "two", "three"}; move_iterator<vector<string>::iterator> mov; |
move_iterator(iterator_type itr) | Constructs a move_iterator from itr. Example: vector<string> v{"one", "two", "three"};//*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)); |
move_iterator(const move_iterator& r) | Copy constructor. Example: vector<string> v{"one", "two", "three"}; //*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)); //*mov="one" *mov2="one" move_iterator<vector<string>::iterator> mov2(mov); |
Overloaded operators
Name | Example |
---|---|
reference operator*() | Returns reference to the element at current position of the reverse iterator that can be used to read or write. Example: vector<string> v{"one", "two", "three"}; //*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)); //prints "one" cout << *mov;//s = "one" *mov="" auto s = *mov; |
|
|
Example:
| |
|
|
Example: vector<string> v{"one", "two", "three"}; //*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)), mov2; //*mov="two" ++mov; //pre -- //mov2="two" mov="one" mov2 = mov--; //post -- //mov2="one" mov="one" mov = --mov2; | |
move_iterator& operator=(const move_iterator<Iter2>& mov) | Assigns iterator. Example: vector<pair<string,string>> v{{"one", "two"}};move_iterator<vector<pair<string,string>>::iterator> mov; |
pointer operator->() | Returns member of the element from a class object, Example: vector<pair<string,string>> v{{"one", "two"}}; move_iterator<vector<pair<string,string>>::iterator> mov = move_iterator(begin(v)); //s= "one" p:{"one", "two"} auto s = mov->first; //p:{"one", "two"} *mov={"",""} auto p = *mov; |
/*unspecified*/ operator[] (difference_type n) | Returns reference to the element after adding in the index. Example: vector<string> v{"one", "two", "three"}; //*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)); //*mov="two" ++mov; //[] //s="two" s2="one" s3="three" //mov[0]="" mov[-1]="" mov[1]="" auto s=mov[0], s2=mov[-1], s3=mov[1]; |
move_iterator operator+ (difference_type n) | Returns iterator after adding index to the iterator. Example: vector<string> v{"one", "two", "three"};//*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)), mov2; //+ //*mov2="two" mov2 = mov+1; |
move_iterator operator- (difference_type n) | Returns iterator after subtracting index to the iterator. Example: vector<string> v{"one", "two", "three"}; //*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)), mov2;//*mov="two" ++mov;//- //*mov2="one" mov2 = mov-1; |
move_iterator operator-= (difference_type n) | Subtracts index to self. Example: vector<string> v{"one", "two", "three"}; //*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v));//*mov="two" ++mov;//-= //*mov="one" mov-= 1; |
move_iterator operator+= (difference_type n) | Adds index to self. Example: vector<string> v{"one", "two", "three"}; //*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)); |
Iterator
Name | Description |
---|---|
iterator_type base() | Returns a copy of the base iterator. vector<string> v{"one", "two", "three"};//*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v));//s = "one", *mov="one" auto s = *mov.base(); |
External overloaded operators
Name | Description |
---|---|
move_iterator <Iterator> operator+ (difference_type n, const move_iterator <Iterator>& rev_it) | Returns move iterator after adding n to the iterator. Example:
|
difference_type operator-(const move_iterator <Iterator>& first_it, const move_iterator <Iterator>& second_it) | Returns distance between two move iterators. i.e., second_it-first_it. Example: vector<string> v{"one", "two", "three"};//*mov="one" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)), mov2 = move_iterator(end(v)); //d=3 auto d = mov2-mov; cout << d; |
| Relational Operators. Example: vector<string> v{"one", "two", "three"};//*mov="one" mov2="" move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)), mov2 = move_iterator(end(v));//== != < > >= <= //mov == mov2 F //mov <= mov2 T
|
An utility function to create a move iterator from an legacy iterator which can be a bidirectional iterator or a random-access iterator. .
Syntax
The syntax is as below. Template parameter Iter represents the original iterator for which reverse_iterator is created. Iter can be bidirectional iterator or a random-access iterator.
template < class Iter >move_iterator<Iter> make_move_iterator( Iter i )
The following example shows its usage.
vector<string> v{"one", "two", "three"}; //*mov="one" move_iterator<vector<string>::iterator> mov = make_move_iterator(begin(v)),
No comments:
Post a Comment