I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com

iter_swap

Swap values of objects pointed by two iterators
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\inline \tau) = \inline I\omega\Omega (In the limit).
  • \inline I = Moment of Inertia.
  • \inline \omega = Angular velocity
  • \inline \Omega = Angular velocity of precession.


Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

Definition

The iter_swap() algorithm is defined in the standard header <algorithm> and the implementation is in <algobase.h>.

Interface

#include <algorithm>
template < class ForwardIterator1, class ForwardIterator2 >
   void iter_swap(
      ForwardIterator1 left,
      ForwardIterator2 right
   );

Parameters:

Parameter Description
left One of the forward iterators whose value is to be exchanged
right The second of the forward iterators whose value is to be exchanged

Description

Iter_swap exchange the values pointed to by the two iterators left and right.

Iter_swap is equivalent to swap(*left, *right).

Return Value

None.

Complexity

Iter_swap algorithm has a constant complexity. It performs one copy construction and two assignments.
Example:
Example - iter_swap algorithm
Problem
This program illustrates the use of the STL iter_swap() algorithm to swap integer values that are pointed to by two different iterators that may point into the same vector of integers, or into two different vectors of integers.
Workings
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a1[] = {1, 2, 3, 4, 5};
  vector<int> v1(a1, a1+5);
  cout <<"\nHere are the contents of v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" ";
 
  int a2[] = {2, 4, 6, 8, 10};
  vector<int> v2(a2, a2+5);
  cout <<"\nHere are the contents of v2:\n";
  for (vector<int>::size_type i=0; i<v2.size(); i++)
    cout <<v2.at(i)<<" ";
 
  cout <<"\nFirst we swap the end values in v1.";
  iter_swap(v1.begin(), v1.end()-1);
  cout <<"\nHere are the contents of the revised v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" ";
 
  cout <<"\nThen we swap the middle values in v1 and v2.";
  iter_swap(v1.begin()+2, v2.begin()+2);
  cout <<"\nHere are the contents of the revised v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" ";
 
  cout <<"\nHere are the contents of the revised v2:\n";
  for (vector<int>::size_type i=0; i<v2.size(); i++)
    cout <<v2.at(i)<<" ";
 
  return 0;
}
Solution
Output:

Here are the contents of v1:
1 2 3 4 5

Here are the contents of v2:
2 4 6 8 10

First we swap the end values in v1.
Here are the contents of the revised v1:
5 2 3 4 1

Then we swap the middle values in v1 and v2.
Here are the contents of the revised v1:
5 2 6 4 1

Here are the contents of the revised v2:
2 4 3 8 10
References