C++. STL. Algorithms. Algorithms for exchanging values of sequence elements

Modifier algorithms. Part 2
Algorithms for exchanging values of sequence elements


Contents


Search other resources:

1. Algorithm swap. Exchange of values of two variables (objects)

To exchange the values of two objects or arrays, the swap algorithm is used. The algorithm has several implementations, the most common of which are as follows

template <class Type>
void swap(
  Type& left,
  Type& right);
  template <class Type, size_t N>
  void swap(
  Type (& left)[N],
  Type (& right)[N]);

where

  • left, right – objects whose values need to be swapped;
  • N – size of arrays of objects.

Example.

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

int main()
{
  // The swap algorithm - exchange of values
  // 1. Обмен типов int
  int a = 8, b = 9;
  swap(a, b);
  cout << "a = " << a << ", b = " << b << endl; // a = 9, b = 8

  // 2. Swapping fixed size arrays
  int A1[] = { 1, 5, 3 };
  int A2[] = { 2, 8, 4 };

  swap(A1, A2);

  cout << "A1 => ";
  for (int i : A1)
    cout << i << " ";
  cout << endl;

  cout << "A2 => ";
  for (int i : A2)
    cout << i << " ";
  cout << endl;

  // 3. Exchange of unique_ptr<T> pointer values
  unique_ptr<int> p1(new int(38));
  unique_ptr<int> p2(new int(29));
  swap(p1, p2);
  cout << "p1->" << *p1 << endl;
  cout << "p2->" << *p2 << endl;

  // 4. Exchange of shared_ptr<T> pointer values
  shared_ptr<int> ps1(new int(100));
  shared_ptr<int> ps2(new int(50));
  swap(ps1, ps2);
  cout << "ps1->" << *ps1 << endl;
  cout << "ps2->" << *ps2 << endl;

  // 5. Exchange of vector values
  vector<int> v1 = { 1, 3, 8, 4 };
  vector<int> v2 = { 7, 9, 0 };
  swap(v1, v2);
  cout << "v1 => ";
  for (int i : v1)
    cout << i << " ";
  cout << endl;
  cout << "v2 => ";
  for (int i : v2)
    cout << i << " ";
  cout << endl;
}

Result

a = 9, b = 8
A1 => 2 8 4
A2 => 1 5 3
p1->29
p2->38
ps1->50
ps2->100
v1 => 7 9 0
v2 => 1 3 8 4

 

2. Algorithm swap_ranges. Swap ranges of elements of two sequences

The swap_ranges algorithm swaps the elements of two ranges. The most common implementation of the algorithm is as follows

template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges(
  ForwardIterator1 first1,
  ForwardIterator1 last1,
  ForwardIterator2 first2 );

where

  • first1, last1 – direct iterators specifying the first range of elements;
  • first2 – forward iterator that specifies the beginning of the second range of elements.

Example.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
  // Algorithm swap_ranges - swaps elements of a given range

  // 1. Declare 2 vectors and display them on the screen
  vector<char> V1 = { 'a', 'b', 'c', 'd', 'e', 'f' };
  vector<char> V2 = { '0', '1', '2', '3', '4' };

  cout << "V1 => ";
  for (char c : V1)
    cout << c << " ";
  cout << endl;

  cout << "V2 => ";
  for (char c : V2)
    cout << c << " ";
  cout << endl;
  cout << "-------------------" << endl;

  // 2. Create iterators to the beginning and end of the range of vector V1
  vector<char>::iterator itBegin = V1.begin();
  vector<char>::iterator itEnd = V1.begin();
  itEnd++;
  itEnd++; // itEnd -> 'c'

  // 3. Create iterators to the beginning and end of the range of vector V2
  vector<char>::iterator itBegin2 = V2.begin();
  itBegin2 += 3; // itBegin2 -> '3'

  // 4. Call the algorithm
  swap_ranges(itBegin, itEnd, itBegin2);

  // 5. Output vector V1
  cout << "V1 => ";
  for (char c : V1)
    cout << c << " ";
  cout << endl;

  // 6. Output vector V2
  cout << "V2 => ";
  for (char c : V2)
    cout << c << " ";
  cout << endl;
}

Result

V1 => a b c d e f
V2 => 0 1 2 3 4
-------------------
V1 => 3 4 c d e f
V2 => 0 1 2 a b

 

3. Algorithm iter_swap. Swapping two values pointed to by iterators

The iter_swap algorithm swaps two values pointed to by iterators. According to the documentation, the algorithm template is as follows

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

where

  • left – iterator to the first value (the value placed on the left);
  • right – an iterator to the second value (the value placed on the right).

Example.

#include <iostream>
#include <queue>
#include <vector>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;

int main()
{
  // Algorithm iter_swap - swaps two values pointed to by iterators

  // 1. Declare the vector
  vector<double> V1 = { 1.1, 2.2, 2.7, 2.8, 3.2 };

  // 2. Display the vector
  cout << "V1 => ";
  for (double x : V1)
    cout << x << " ";
  cout << endl;

  // 3. Set iterators to first and last element of vector
  vector<double>::iterator it1 = V1.begin();
  vector<double>::iterator it2 = V1.begin();
  while (it2 != V1.end()) it2++;
  it2--;

  // 4. Swap - the iter_swap algorithm
  iter_swap(it1, it2);

  // 5. Re-output vector V1
  cout << "V1 => ";
  for (double x : V1)
    cout << x << " ";
  cout << endl;
}

Result

V1 =&gt; 1.1 2.2 2.7 2.8 3.2
V1 => 3.2 2.2 2.7 2.8 1.1

 


Related topics