Thursday, October 17, 2024

move_iterator adaptor

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 typedefinition
iterator_typeIter
iterator_categoryiterator_traits<Iter>::iterator_category
value_typeiterator_traits<Iter>::value_type
difference_typeiterator_traits<Iter>::difference_type
pointeriterator_traits<Iter>::pointer
referencevalue_type&&

Operation
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
NameDescription
move_iterator()
Default constructor

Example:
vector<string> v{"one", "two", "three"};

move_iterator<vector<string>::iterator> mov;

//*mov="one"
mov = move_iterator(begin(v));
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
NameExample
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;
  1. move_iterator& operator++()
  2. move_iteratoroperator++(int)
  1. Returns a reference to self after incrementing of its position. 
  2. Returns copy of self and increments its position. returns new reference 
Example:
vector<string> v{"one", "two", "three"};
//*mov="one"
move_iterator<vector<string>::iterator> mov = move_iterator(begin(v)), mov2;

//pre ++
//mov="two" mov2="one"
mov2 = mov++;
    
//post ++
//mov="two" mov2="two"
mov = ++mov2;
  1. move_iterator& operator--()
  2. move_iteratoroperator--(int)
  1. Returns a reference to self after decrementing of its position. 
  2. Returns copy of self and decrements its position.eturns reference 
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_iteratoroperator=(const move_iterator<Iter2>& movAssigns iterator.

Example:
vector<pair<string,string>> v{{"one", "two"}};
move_iterator<vector<pair<string,string>>::iterator> mov;

//*mov = "one"
mov = move_iterator(begin(v));
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));

//+=
//*mov="two" mov+= 1;

Iterator
NameDescription
iterator_type base()

Returns a copy of the base iterator.

Example:
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
NameDescription
move_iterator <Iterator> operator+ (difference_type n,
const move_iterator <Iterator>& rev_it)
Returns move iterator after adding n 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 = 1 + mov;
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;
  1. bool operator== (const move_iterator <iter>& lhs,
    const move_iterator <iter2>& rhs)
  2. bool operator!= (const move_iterator <iter>& lhs,
    const move_iterator <iter2>& rhs)
  3. bool operator> (const move_iterator <iter>& lhs,
    const move_iterator <iter2>& rhs)
  4. bool operator<(const move_iterator <iter>& lhs,
    const move_iterator <iter2>& rhs)
  5. bool operator>=(const move_iterator <iter>& lhs,
    const move_iterator <iter2>& rhs)
  6. bool operator<=(const move_iterator <iter>& lhs,
    const move_iterator <iter2>& rhs)
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
//mov > mov2 F
//mov >= mov2 F
//mov < mov2 T
//mov <= mov2 T


make_move_iterator
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