C++/STL Algorithms Modifying sequence operations/swap

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Illustrating the generic swap algorithm

 
 
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
  vector<int> vector1(100, 1), vector2(200, 2);
  swap(vector1, vector2);

  for(int i=0;i<vector1.size();i++){
     cout << vector1[i] ;
  }
  cout << "\n\n\n\n\n";
  for(int i=0;i<vector2.size();i++){
     cout << vector2[i] ;
  }
  return 0;
}
/* 
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222


11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111
 */


Illustrating the generic swap algorithm: swap two integers

 
 

#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
  int high = 250, low = 0;
  swap(high, low);
  cout << high << " ";
  cout << low;

  return 0;
}
/* 
0 250
 */


Swap elements at locations 0 and 1 of an array

 
 
#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
#include <iterator>
int main()
{
   int a[ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   std::ostream_iterator< int > output( cout, " " );
   cout << "Array a contains:\n   ";
   std::copy( a, a + 10, output );
   
   std::swap( a[ 0 ], a[ 1 ] );
   cout << "\nArray a after swapping a[0] and a[1] using swap:\n   ";
   std::copy( a, a + 10, output ); // display array a
   cout << endl;
   return 0;
}
/* 
Array a contains:
   1 2 3 4 5 6 7 8 9 10
Array a after swapping a[0] and a[1] using swap:
   2 1 3 4 5 6 7 8 9 10
 */


Swap second and fourth element in a vector

 
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
    // create empty vector for strings
    vector<string> sentence;
    // reserve memory for five elements to avoid reallocation
    sentence.reserve(5);
    // append some elements
    sentence.push_back("Hello,");
    sentence.push_back("how");
    sentence.push_back("are");
    sentence.push_back("you");
    sentence.push_back("?");

    // swap second and fourth element
    swap (sentence[1], sentence[3]);
    // print elements separated with spaces
    copy (sentence.begin(), sentence.end(),
          ostream_iterator<string>(cout," "));
    cout << endl;
}
/* 
Hello, you are how ?
 */