C++ STL algorithm, iter_swap() program sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ iter_swap() to exchange two values referred to by a pair of specified iterators in C++ programming

To show: How to use the C++ algorithm, iter_swap() to exchange two values referred to by a pair of specified iterators in C++ programming

 

// C++ STL algorithm, iter_swap()

#include <vector>

#include <deque>

#include <algorithm>

#include <iostream>

using namespace std;

 

class CInt;

ostream& operator<<(ostream& osIn, const CInt& rhs);

class CInt

{

public:

CInt(int n = 0) : m_nVal(n){}

CInt(const CInt& rhs) : m_nVal(rhs.m_nVal){}

CInt& operator=(const CInt& rhs)

{ m_nVal = rhs.m_nVal; return *this;}

bool operator<(const CInt& rhs) const

{ return (m_nVal < rhs.m_nVal);}

friend ostream& operator<<(ostream& osIn, const CInt& rhs);

private:

int m_nVal;

};

 

inline ostream& operator<<(ostream& osIn, const CInt& rhs)

{

osIn<<"CInt(" <<rhs.m_nVal<< ")";

return osIn;

}

 

// return whether modulus of elem1 is less than modulus of elem2

bool mod_lesser(int elem1, int elem2)

{

if(elem1 < 0)

elem1 = - elem1;

if(elem2 < 0)

elem2 = - elem2;

return (elem1 < elem2);

};

 

int main(void)

{

CInt c1 = 9, c2 = 12, c3 = 17;

 

// deque container

deque<CInt> deq;

// deque iterator

deque<CInt>::iterator deqIter;

 

// pushing/inserting data

deq.push_back(c1);

deq.push_back(c2);

deq.push_back(c3);

 

// printing data

cout<<"The deque of CInts data is: ";

for(deqIter = deq.begin(); deqIter != --deq.end(); deqIter++)

cout<<" "<<*deqIter<<",";

deqIter = --deq.end();

cout<<" "<<*deqIter<<endl;

 

// exchanging first and last elements with iter_swap()

iter_swap(deq.begin(), --deq.end());

cout<<"\nThe deque of CInts data with first and last elements swapped is: ";

for(deqIter = deq.begin(); deqIter != --deq.end(); deqIter++)

cout<<" "<<*deqIter<<",";

deqIter = --deq.end();

cout<<" "<<*deqIter<<endl;

// swapping back first and last elements with swap()

swap(*deq.begin(), *(deq.end() -1));

cout<<"\nThe deque of CInts data with first and last elements re-swapped is: ";

for(deqIter = deq.begin(); deqIter != --deq.end(); deqIter++)

cout<<" "<<*deqIter<<",";

deqIter = --deq.end();

cout<<" "<<*deqIter<<endl;

 

// swapping a vector element with a deque element

//

// vector container

vector <int> vec;

// vector iterator

vector <int>::iterator Iter1;

// deque container

deque <int> deq1;

// deque iterator

deque <int>::iterator deq1Iter;

int i, j;

 

// pushing data

for(i = 10; i <= 14; i++)

vec.push_back(i);

for(j = 16; j <= 20; j++)

deq1.push_back(j);

 

// printing data

cout<<"\nvec vector data: ";

for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

cout<<"\ndeq1 deque data: ";

for(deq1Iter = deq1.begin(); deq1Iter != deq1.end(); deq1Iter++)

cout<<*deq1Iter<<" ";

cout<<endl;

iter_swap(vec.begin(), deq1.begin());

cout<<"\nAfter exchanging first elements, vec vector data is: ";

for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<"and deq1 deque data is: ";

for(deq1Iter = deq1.begin(); deq1Iter != deq1.end(); deq1Iter++)

cout<<*deq1Iter<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

The deque of CInts data is: CInt(9), CInt(12), CInt(17)

The deque of CInts data with first and last elements swapped is: CInt(17), CInt(12), CInt(9)

The deque of CInts data with first and last elements re-swapped is: CInt(9), CInt(12), CInt(17)

vec vector data: 10 11 12 13 14

deq1 deque data: 16 17 18 19 20

After exchanging first elements, vec vector data is: 16 11 12 13 14 and deq1 deque data is: 10 17 18 19 20

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index