C++ Iterator Library - move_iterator



Description

It is an iterator adaptor which behaves exactly like the underlying iterator.

Declaration

Following is the declaration for std::move_iterator.

C++11

template <class Iterator> class move_iterator;

Parameters

Iterator − It is a bidirectional iterator type.

Return value

none

Exceptions

If x somehow throws while applying the unary operator& to it, this function never throws exceptions.

Time complexity

constant for random-access iterators.

Example

The following example shows the usage of std::move_iterator.

#include <iostream>     
#include <iterator>     
#include <vector>       
#include <string>       
#include <algorithm>    

int main () {
   std::vector<std::string> foo (3);
   std::vector<std::string> bar {"sai","ram","krishna"};

   typedef std::vector<std::string>::iterator Iter;

   std::copy ( std::move_iterator<Iter>(bar.begin()),
               std::move_iterator<Iter>(bar.end()),
               foo.begin() );

   bar.clear();

   std::cout << "foo:";
   for (std::string& x : foo) std::cout << ' ' << x;
   std::cout << '\n';

   return 0;
}

Let us compile and run the above program, this will produce the following result −

foo: sai ram krishna
iterator.htm
Advertisements