I have forgotten
my Password

Or login with:

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

swap

Exchanges the values of the elements between two types of objects
+ 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 swap() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template <class Type>
   void swap(
      Type& left, 
      Type& right
   );

Parameters:
Parameter Description
left The first object to have its elements exchanged
right The second object to have its elements exchanged

Description

Swap assigns the contents of left to right and the contents of right to left.

Return Value

None.

Complexity

Swap algorithm has a constant complexity. It performs one copy construction and two assignments.

Example:
Example - swap algorithm
Problem
This program illustrates the functionality of swap algorithm.
Workings
#include <vector>
#include <algorithm>
#include <iostream>
 
using namespace std;
 
bool greaterthan(int value)
{  return value > 5;
}
 
int main()
{
  vector <int> vec1, vec2;
  vector <int>::iterator Iter1, Iter2, result;
  int i;
  for (i = 10; i<= 20; i++)
    vec1.push_back(i);
  int j;
  for(j = 10; j <= 15; j++)
    vec2.push_back(j);
  cout<<"Vector vec1 data is:\n";
  for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
  cout <<"\nVector vec2 data is:\n";
  for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)
    cout<<*Iter2<<" ";
  cout <<endl;
  swap(vec1, vec2);
  cout <<"\nNow, vector vec1 data is:\n";
  for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
  cout <<"\nThen, vector vec2 data is:\n";
  for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)
    cout <<*Iter2<<" ";
  cout <<endl;
 
  return 0;
}
Solution
Output:

Vector vec1 data is:
10 11 12 13 14 15 16 17 18 19 20

Vector vec2 data is:
10 11 12 13 14 15

Now, vector vec1 data is:
10 11 12 13 14 15

Then, vector vec2 data is:
10 11 12 13 14 15 16 17 18 19 20
References