C++. STL. Algorithms processing the sequence as a whole

Algorithms processing the sequence as a whole without changing the values of the elements (reversing, cyclic shifting, copying, mixing)


Contents


Search other contents:

1. Algorithm copy. Copies one sequence to another. Example for classes vector, list
The copy algorithm implements copying one sequence to another. A common implementation has the following declaration
template<class InputIterator, class OutputIterator>
OutputIterator copy(
    InputIterator first,
    InputIterator last,
    OutputIterator destBeg);
here
  • first, last – input iterators that implement the range of numbers to be copied;
  • destBeg – output iterator that specifies the beginning of the range into which the source range is copied.
Example.
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;

void main()
{
  // Algorithm copy
  // 1. Demo for the vector class
  // 1.1. Create initialization list
  initializer_list<double> IL1 = { 2.8, 3.5, 1.4, 2.9, 7.3 };

  // 1.2. Create a vector instance based on L1 list
  vector<double> V1(IL1);

  // 1.3. Create a new destination array, the array V1 will be copied into this array
  vector<double> V2(V1.size()); // it is necessary to allocate memory in V2

  // 1.4. copy V1 => V2, use the copy algorithm
  copy(V1.begin(), V1.end(), V2.begin());

  // 1.5. Output array V2
  cout << "V2 => ";
  for (double t : V2)  // foreach style loop
    cout << t << "  ";
  cout << endl;

  // ---------------------------------------
  // 2. Demonstration for the list class
  // 2.1. Create source array
  initializer_list<int> IL2 = { 2, 3, 8, 1, 4, 9 };
  list<int> L1(IL2);

  // 2.2. Create destination array
  list<int> L2(L1.size());

  // 2.3. Copy lst2 <= lst1
  copy(L1.begin(), L1.end(), L2.begin());

  // 2.4. Output lst2 using iterator.
  list<int>::iterator it = L2.begin();
  cout << "L2 => ";
  while (it != L2.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;
}
Result
V2 => 2.8  3.5  1.4  2.9  7.3
L2 => 2 3 8 1 4 9

 

2. Algorithm copy_backward. Copies the sequence in reverse order
The copy_backward algorithm copies a sequence from a source range to a destination range in reverse order. The declaration of the algorithm is as follows
template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward(
    BidirectionalIterator1 first,
    BidirectionalIterator1 last,
    BidirectionalIterator2 destEnd);
here
  • first, last – bidirectional iterators indicating the beginning and end of the source range;
  • destEnd is a bidirectional iterator pointing to the start of the destination range.
Example.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

void main()
{
  // copy_backward algorithm - copy data in reverse order.
  // Usage for class vector
  // 1. Create array based on initialization list
  initializer_list<int> IL = { 2, 3, 8, 1, 6 };
  vector<int> V(IL);

  // 2. Create a new array based on the size of the first one
  vector<int> V2(V.size());

  // 3. Call the copy_backward algorithm, the data is copied to the V2 vector from the end
  copy_backward(V.begin(), V.end(), V2.end());

  // 4. Output array V2
  vector<int>::iterator it = V2.begin();
  while (it != V2.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;
}
Result
2 3 8 1 6

 

3. Algorithm reverse. Reverse the order of elements (reversal)
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <time.h>
#include <list>
using namespace std;

int main()
{
  // The reverse algorithm - reverse the order of the elements (reversal)
  // 1. Create an array of numbers
  vector<double> V = { 2.8, 3.3, 4.5, 1.7, 2.9 };

  // 2. Reverse the array V - the reverse algorithm.
  reverse(V.begin(), V.end());

  // 3. Output the reversed array
  for (int i = 0; i < V.size(); i++)
    cout << V[i] << " ";
  cout << endl;
}
Result
2.9 1.7 4.5 3.3 2.8

 

4. Algorithm reverse_copy. Reverse array to get a copy
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <time.h>
#include <list>
using namespace std;

int main()
{
  // The reverse_copy algorithm - reverse the array to get a copy.
  // 1. Create an array of numbers
  vector<double> V1 = { 1.1, 2.2, 3.3, 4.4, 5.5 }; // array-original
  vector<double> V2(V1.size()); // array-copy - set the size of the array (required)

  // 2. Reverse array V1 to get a copy of V3 - reverse_copy algorithm
  reverse_copy(V1.begin(), V1.end(), V2.begin());

  // 3. Output array-original
  cout << "V1 => ";
  vector<double>::iterator it = V1.begin();
  while (it != V1.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;

  // 4. Output the array-copy
  cout << "V2 => ";
  it = V2.begin();
  while (it != V2.end())
  {
    cout << *it << " ";
    it++;
  }
}
Result
V1 => 1.1 2.2 3.3 4.4 5.5
V2 => 5.5 4.4 3.3 2.2 1.1

 

5. Algorithm rotate. Cyclic shift within the specified limits
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
  // Algorithm rotate - cyclic shift to the left within the given limits
  // 1. Create an array of numbers
  vector<double> V1 = { 1.1, 2.2, 3.3, 4.4, 5.5 }; // array-original

  // 2. Set the point to which the offset will occur,
  // this point is within V1.begin() to V1.end().
  vector<double>::iterator it = V1.begin();

  it++; // the next point after V1.begin() is position 2.2

  // 3. Shift the entire vector to this point - the rotate algorithm
  rotate(V1.begin(), it, V1.end());

  // 4. Output the original array
  cout << "V1 => ";
  it = V1.begin();
  while (it != V1.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;
}
Result
V1 => 2.2 3.3 4.4 5.5 1.1

 

6. Algorithm rotate_copy. Cyclic shift to obtain a copy of the sequence
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
  // The rotate_copy algorithm - cyclic shift to get a copy of the sequence
  // 1. Создать массив чисел
  vector<double> V1 = { 1.1, 2.2, 3.3, 4.4, 5.5 }; // array-original
  vector<double> V2(V1.size());

  // 2. Specify the point to which the offset will occur, this point in the range from V1.begin() to V1.end().
  vector<double>::iterator it = V1.begin();
  it++;
  it++; // set the iterator at position 3.3

  // 3. Shift the entire vector to this point - the rotate_copy algorithm
  rotate_copy(V1.begin(), it, V1.end(), V2.begin());

  // 4. Output the copy array
  cout << "V2 => ";
  it = V2.begin();
  while (it != V2.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;
}
Result
V2 => 3.3 4.4 5.5 1.1 2.2

 

7. Algorithm random_shuffle (changed to shuffle). Shuffle sequence randomly
#include <iostream>
#include <queue>
#include <vector>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;

int main()
{
  // Algorithm random_shuffle - mixing (reordering) the elements of a sequence
  // 1. Using the algorithm without predicate
  // 1.1. Declare a vector and form it
  vector<float> V1;

  for (int i = 0; i < 10; i++)
    V1.push_back(i * 1.0f);

  // 1.2. Output vector
  cout << "V1 => ";
  for (float x : V1)
    cout << x << "  ";
  cout << endl;

  // 1.3. Shuffle elements
  random_shuffle(V1.begin(), V1.end());

  // 1.4. Re-output the vector
  cout << "V1 => ";
  for (float x : V1)
    cout << x << "  ";
  cout << endl;
}
Result
V1 => 0  1  2  3  4  5  6  7  8  9
V1 => 8  1  9  2  0  5  7  3  4  6

 


Related topics