Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 2:36 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: list swap in C++
PostPosted: Thu Nov 13, 2008 7:18 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

Code:
#include <iostream>
using std::cout;
using std::endl;

#include <list>      // list class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator

int main()
{
   int array[ 4 ] = { 2, 6, 4, 8 };
   std::list< int > values;      // create list of ints
   std::list< int > otherValues; // create list of ints
   std::ostream_iterator< int > output( cout, " " );

   // insert items in values
   values.push_front( 1 );
   values.push_front( 3 );
   values.push_back( 4 );
   values.push_back( 2 );

   cout << "values contains: ";
   std::copy( values.begin(), values.end(), output );

   otherValues.insert( otherValues.begin(), array, array + 4 );
   cout << "\n\notherValues contains: ";
   std::copy( otherValues.begin(), otherValues.end(), output );

   values.swap( otherValues );

   cout << "\n\nvalues contains: ";
   std::copy( values.begin(), values.end(), output );

   cout << "\n\notherValues contains: ";
   std::copy( otherValues.begin(), otherValues.end(), output );

   cout << endl;
   return 0;
}

/*
values contains: 3 1 4 2

otherValues contains: 2 6 4 8

values contains: 2 6 4 8

otherValues contains: 3 1 4 2

*/       
 


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 methods with variable arguments list  Java examples  msi_333  1
 can u check tis code to perform union & intersection of list  C-C++  Anonymous  1
 How to write a PHP coding to list out all files and director  PHP  Andrrew  2
 fill selection options from list  ASP/ASP.net examples  msi_333  0
 X'mas wish list!  General Discussion  isuru  2

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team