C++. STLModifying methods. Part 4 Algorithms that extract individual elements or groups of elements from a sequence

Modifying methods. Part 4
Algorithms that extract individual elements or groups of elements from a sequence


Contents


Search other resources:

1. Algorithm remove. Remove an element from a sequence based on its value

 

#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
#include <functional>
#include <time.h>
using namespace std;


void main()
{
  // The remove algorithm is to remove an element from a sequence based on its value.
  // 1. Create a list of random numbers within [1; 20]
  list<int> l;
  srand(time(NULL));
  for (int i = 0; i < 15; i++)
    l.push_back(rand() % (20 - 1 + 1) + 1);

  // 2. Output the generated list
  cout << "List of integers:\n";

  // 2.1. Declare an iterator
  list<int>::iterator it;

  // 2.2. Set the iterator to the first element of a list
  it = l.begin();

  // 2.3. Loop to display the list-original
  while (it != l.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl << endl;

  // 3. Using the remove algorithm.
  // Delete all elements that are equal to 5.
  // 3.1. Calculate the number of elements that are equal to 5 - use the count algorithm
  int value_5 = count(l.begin(), l.end(), 5);

  // 3.2. Remove elements equal to 5 - call the remove algorithm
  remove(l.begin(), l.end(), 5);

  // 3.3. Resize the sequence l.
  l.resize(l.size() - value_5);

  // 4. Output a new result sequence.
  cout << "A new sequence: " << endl;
  it = l.begin();
  while (it != l.end())
  {
    cout << *it << " ";
    it++;
  }
}

Result

List of integers:
10 20 5 12 3 7 12 13 4 20 2 6 18 10 3

A new sequence:
10 20 12 3 7 12 13 4 20 2 6 18 10 3

 

2. The remove_if algorithm. Remove elements from a sequence that satisfy a given condition.

 

#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
#include <functional>
#include <time.h>
using namespace std;

// A predicate that determines if a number is greater than 10
bool More_Than_10(int value)
{
  return value > 10;
}

void main()
{
  // The remove_if algorithm removes all elements from a sequence that match a given value.
  // 1. Create a list of random numbers within [1; 20]
  list<int> l;
  srand(time(NULL));
  for (int i = 0; i < 15; i++)
    l.push_back(rand() % (20 - 1 + 1) + 1);

  // 2. Output the generated list
  cout << "List of integers:\n";

  // 2.1. Declare an iterator
  list<int>::iterator it;

  // 2.2. Set the iterator to the first element of the list
  it = l.begin();

  // 2.3. Loop to output list-original
  while (it != l.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl << endl;

  // 3. Using the remove_if algorithm.
  // Remove all elements that are greater than 10.
  // More_Than_10 predicate is set
  // 3.1. Calculate the number of elements that are greater than 10 - the count_if algorithm.
  int count = count_if(l.begin(), l.end(), More_Than_10);

  // 3.2. Generate a new list - the size of the list remains 15
  remove_if(l.begin(), l.end(), More_Than_10);

  // 3.3. Resize list
  l.resize(l.size() - count);

  // 4. Output a new result sequence
  cout << "A new sequence: " << endl;
  it = l.begin();
  while (it != l.end())
  {
    cout << *it << " ";
    it++;
  }

  // 5. Display new list size
  cout << endl << l.size() << endl;
}

Result

List of integers:
4 3 12 14 16 12 7 13 2 18 2 18 9 12 13

A new sequence:
4 3 7 2 2 9
6

 

3. Algotirhm remove_copy. Removing elements from a sequence and creating a new sequence.

 

#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
#include <functional>
#include <time.h>
using namespace std;

void main()
{
  // Algorithm remove_copy.
  // The algorithm removes elements from the given range and creates a new copy of the sequence.

  // 1. Create a list of random characters
  list<char> l;
  srand(time(NULL));
  for (int i = 0; i < 15; i++)
    l.push_back((int)'a' + rand() % 26);

  // 2. Output the generated list
  cout << "List of characters:\n";

  // 2.1. Declare an iterator
  list<char>::iterator it;

  // 2.2. Set the iterator to the first element of the list
  it = l.begin();

  // 2.3. Loop to display the list-original
  while (it != l.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl << endl;

  // 3. Using the remove_copy() algorithm.
  // Delete all 'f' characters
  // 3.1. Declare a new list of the same size as list l
  list<char> l2(l.size());

  // 3.2. Call the remove_copy algorithm
  remove_copy(l.begin(), l.end(), l2.begin(), 'f');

  // 3.3. Output a new result sequence
  cout << "A new sequence: " << endl;
  it = l2.begin();
  for (int i = 0; i < l2.size(); i++)
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;
}

Result

List of characters:
l o s f q q h f q t u m j y j

A new sequence:
l o s q q h q t u m j y j

 

4. Algorithm remove_copy_if. Remove elements from a sequence based on a specified condition, creating a copy of the sequence

 

#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
#include <functional>
#include <time.h>
using namespace std;

// A predicate that determines whether a value is less than 5.
bool LessThan_5(int value)
{
  return value < 5;
}

void main()
{
  // Algorithm remove_copy_if.
  // The algorithm removes elements from a given range that meet a given condition.
  // The algorithm creates a new sequence-copy.

  // 1. Create a list of random numbers within [1; 20]
  list<int> l;
  srand(time(NULL));
  for (int i = 0; i < 15; i++)
    l.push_back(rand() % (20 - 1 + 1) + 1);

  // 2. Output the generated list
  cout << "List of integers:\n";

  // 2.1. Declare an iterator
  list<int>::iterator it;

  // 2.2. Set iterator to first element of list
  it = l.begin();

  // 2.3. Loop to display the list-original
  while (it != l.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl << endl;

  // 3. Using the remove_copy_if() algorithm.
  // Remove all elements that are less than 5.
  // 3.1. Declare a new list of the same size as list l.
  list<int> l2(l.size());

  // 3.2. Call the remove_copy_if algorithm - generate a list of elements
  // that have a value greater than 5. For this, the MoreThan_5 predicate is used.
  remove_copy_if(l.begin(), l.end(), l2.begin(), LessThan_5);

  // 3.3. Output a new result sequence
  cout << "A new sequence: " << endl;
  it = l2.begin();
  for (int i = 0; i < l2.size(); i++)
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;

  // 4. Using the remove_copy_if() algorithm in combination with a lambda expression.
  // 4.1. Declare a new list of the same size as list l.
  list<int> l3(l.size());

  // 4.2. Call the algorithm with a lambda expression.
  remove_copy_if(l.begin(), l.end(), l3.begin(), [](int value) { return value < 5; });

  // 4.3. Display the result
  cout << "A new sequence: " << endl;
  it = l3.begin();
  while (it != l3.end())
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;
}

Result

List of integers:
5 11 1 6 19 3 20 8 18 11 3 2 14 19 2

A new sequence:
5 11 6 19 20 8 18 11 14 19 0 0 0 0 0
A new sequence:
5 11 6 19 20 8 18 11 14 19 0 0 0 0 0

As can be seen from the result, this algorithm does not change the size of the resulting sequence. The size can be set using the count_if algorithm and the resize() method of the list<T> container (see how it’s done in the remove and remove_if algorithms).

 

5. Algorithm unique. Extract duplicates from a sequence

 

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

// A predicate that determines the equality of elements
bool Equal_Items(int a, int b)
{
  // the next element is greater than the previous one by 1
  return a + 1 == b;
}

int main()
{
  // The unique algorithm - extract duplicates from sequence
  // { ... 3, 3, 3, ... } => { ... 3 ... }

  // 1. Using unique without a predicate
  // 1.1 Create a sequence of numbers and print it to the console
  list<int> L1 = { 2, 8, 2, 2, 3, 4, 5, 8, 1 };
  cout << "L1 => ";
  for (int i : L1)
    cout << i << " ";
  cout << endl;

  // 1.2. Declare an iterator pointing to the end of a new sequence
  list<int>::iterator endL1;

  // 1.3. Create another sequence that does not contain duplicates
  endL1 = unique(L1.begin(), L1.end());

  // 1.4. Output the resulting sequence L1 up to the iterator endL1
  cout << "L1+ => ";
  list<int>::iterator it = L1.begin();
  while (it != endL1)
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;

  // 2. Using the algorithm with a predicate
  // 2.1. Create a sequence of numbers and print it to the console
  list<int> L2 = { 2, 8, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 8, 5 };
  cout << "L2 => ";
  for (int i : L2)
    cout << i << " ";
  cout << endl;

  // 2.2. Declare the iterator that will point to the end of the new sequence.
  list<int>::iterator endL2;

  // 2.3. Call the unique algorithm
  endL2 = unique(L2.begin(), L2.end(), Equal_Items);

  // 2.4. Print the L2 sequence up to the endL2 iterator
  cout << "L2+ => ";
  list<int>::iterator it2 = L2.begin();
  while (it2 != endL2)
  {
    cout << *it2 << " ";
    it2++;
  }
  cout << endl;
}

Result

L1 => 2 8 2 2 3 4 5 8 1
L1+ => 2 8 2 3 4 5 8 1
L2 => 2 8 2 2 2 3 3 4 4 4 5 5 5 5 8 5
L2+ => 2 8 2 2 2 4 4 4 8 5

 

6. The unique_copy algorithm. Extract duplicates from a sequence to get a new sequence

 

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

// Predicate that determines the equality of elements
bool Equal_Items(int a, int b)
{
  // the next element is greater than the previous one by 1
  return a + 1 == b;
}

int main()
{
  // unique_copy algorithm - extract duplicates from a sequence to get a new sequence
  // { ... 3, 3, 3, ... } => { ... 3 ... }

  // 1. Using unique_copy without a predicate
  // 1.1 Create a sequence of numbers and print it to the console
  list<int> L1 = { 2, 8, 2, 2, 2, 3, 3, 4, 5, 8, 1 };
  cout << "L1 => ";
  for (int i : L1)
    cout << i << " ";
  cout << endl;

  // 1.2. Create a sequence in which the result will be written
  list<int> L2(L1.size());

  // 1.3. Declare an iterator pointing to the end of a new sequence
  list<int>::iterator endL2;

  // 1.4. Declare an iterator pointing to the end of a new sequence
  endL2 = unique_copy(L1.begin(), L1.end(), L2.begin());

  // 1.5. Print the resulting L2 sequence up to the iterator endL2
  cout << "L2 => ";
  list<int>::iterator it = L2.begin();
  while (it != endL2)
  {
    cout << *it << " ";
    it++;
  }
  cout << endl;

  // 2. Using the algorithm with a lambda expression
  // 2.1. Create a sequence of numbers and print it to the console
  list<int> L3 = { 2, 8, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 8, 5 };
  cout << "L3 => ";
  for (int i : L3)
    cout << i << " ";
  cout << endl;

  // 2.2. Create a sequence in which the result will be written
  list<int> L4(L3.size());

  // 2.3. Declare an iterator pointing to the end of a new sequence
  list<int>::iterator endL4;

  // 2.4. Call unique_copy algorithm based on lambda expression
  // The lambda expression defines the rule:
  // if the previous element is greater than the next, then combine these 2 elements, the former element remains.
  endL4 = unique_copy(
    L3.begin(), // the beginning of the original sequence
    L3.end(), // end of original sequence
    L4.begin(), // start of the resulting sequence
    [](int a, int b) { return a > b; } // there may also be a corresponding predicate here
  );

  // 2.5. Output sequence L2 up to iterator endL2
  cout << "L2+ => ";
  list<int>::iterator it2 = L4.begin();
  while (it2 != endL4)
  {
    cout << *it2 << " ";
    it2++;
  }
  cout << endl;
}

Result

L1 => 2 8 2 2 2 3 3 4 5 8 1
L2 => 2 8 2 3 4 5 8 1
L3 => 2 8 2 2 2 3 3 4 4 4 5 5 5 5 8 5
L2+ => 2 8 8

 


Related topics