Modifying algorithms. Part 3
Algorithms that replace elements of a sequence
1. Algorithm replace. In the given sequence, replace one value with another
The replace algorithm examines each element in the range and replaces it if it matches the given value. The algorithm declaration looks like this
template <class ForwardIterator, class Type> void replace( ForwardIterator first, ForwardIterator last, const Type& oldVal, const Type& newVal);
here
- first, last – direct iterators specifying the range in which elements are replaced;
- oldVal – old value of replaced elements;
- newVal – new value that replaces the value of oldVal.
Example.
#include <iostream> #include <list> #include <algorithm> #include <time.h> using namespace std; void main() { // The replace algorithm - replaces one value with another // 1. Create a list list<int> l; // Fill list with new values 1..10 for (int i = 0; i < 10; i++) l.push_back(rand() % (10 - 1 + 1) + 1); // 2. Display a list for control list<int>::iterator it = l.begin(); cout << "Old list: " << endl; while (it != l.end()) { cout << *it << " "; it++; } cout << endl; // 3. Demonstration of the replace algorithm. // Replace all 5s with 0s. replace(l.begin(), l.end(), 5, 0); // 4. Display new list cout << "A new list:" << endl; it = l.begin(); while (it != l.end()) { cout << *it << " "; it++; } }
Result
Old list: 2 8 5 1 10 5 9 9 3 5 A new list: 2 8 0 1 10 0 9 9 3 0
2. Algorithm replace_if. In a sequence, replace values with others based on a predicate or lambda expression
The replace_if algorithm checks each element in the range, and if it satisfies the given predicate, then this element is replaced with a new value. The declaration of the algorithm is as follows
template <class ForwardIterator, class UnaryPredicate, class Type> void replace_if( ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const Type& value);
where
- first, last – direct iterators defining a range;
- pred – unary predicate that specifies the execution condition;
- value – new value with which the old value of the element is replaced if the pred predicate is asserted.
Example.
#include <iostream> #include <list> #include <algorithm> #include <time.h> using namespace std; // A predicate that returns true if the number is in the range [3; 7] bool Between_3_7(int value) { return (3 <= value) && (value <= 7); } void main() { // Algorithm replace_if - replaces some values with others based on a predicate or lambda expression // 1. Create a list list<int> l; // Fill list with new values 1..10 srand(time(NULL)); for (int i = 0; i < 10; i++) l.push_back(rand() % (10 - 1 + 1) + 1); // 2. Display list list<int>::iterator it = l.begin(); cout << "Old list: " << endl; while (it != l.end()) { cout << *it << " "; it++; } cout << endl; // 3. Demonstration of the replace algorithm. // Replace all numbers that lie in the range [3; 7] to the number 0. replace_if(l.begin(), l.end(), Between_3_7, 0); // 4. Display the new list cout << "A new list:" << endl; it = l.begin(); while (it != l.end()) { cout << *it << " "; it++; } }
Result
Old list: 2 8 5 1 10 5 9 9 3 5 A new list: 2 8 0 1 10 0 9 9 0 0
3. Algorithm replace_copy_if. Replace characters in a sequence based on a given predicate or lambda expression, creating a new copy sequence
The replace_copy_if algorithm checks each element in the original range and replaces it if that element matches the given criteria. The matching criterion can be specified using a predicate or a lambda expression.
The declaration of the algorithm is as follows
template <class InputIterator, class OutputIterator, class UnaryPredicate, class Type> OutputIterator replace_copy_if( InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred, const Type& value);
where
- first, last – input iterators specifying the initial range;
- result – output iterator pointing to the position of the first element in the destination range. In this range, the elements from the original range are copied;
- pred – unary predicate that defines the criterion for matching the element from the range [first; last-1];
- value – new value that is assigned to the elements.
Example.
#include <iostream> #include <list> #include <algorithm> #include <vector> #include <functional> #include <time.h> using namespace std; void main() { // Algorithm replace_copy_if - Replaces characters in a sequence based on a given predicate. // Creates a new result sequence. // 1. Create a list list<int> l; // Fill list with new values 1..10 for (int i = 0; i < 10; i++) l.push_back(rand() % (10 - 1 + 1) + 1); // 2. Display list list<int>::iterator it = l.begin(); cout << "Old list: " << endl; while (it != l.end()) { cout << *it << " "; it++; } cout << endl; // 3. Demonstration of the replace_copy_if() function // 3.1. Create a new result list list<int> l2(l.size()); // 3.2. Calling the replace_copy_if algorithm using a lambda expression replace_copy_if( l.begin(), l.end(), l2.begin(), [](int value) { return value > 5; }, // lambda expression - value greater than 5 0 ); // replaces all numbers greater than 5 with the number 0 // 4. Display new list cout << "A new list:" << endl; it = l2.begin(); while (it != l2.end()) { cout << *it << " "; it++; } }
Result
Old list: 2 8 5 1 10 5 9 9 3 5 A new list: 2 0 5 1 0 5 0 0 3 5
4. Algorithm replace_copy. Replace some characters in a sequence with other characters, creating a copy of the resulting sequence
The replace_copy algorithm examines each element in the original range and replaces it with a new value. A common implementation of the algorithm has the following declaration
template <class InputIterator, class OutputIterator, class Type> OutputIterator replace_copy( InputIterator first, InputIterator last, OutputIterator result, const Type& oldVal, const Type& newVal);
where
- first, last – input iterators that specify the range in which the element is searched;
- result – output iterator that defines the resulting range;
- oldVal – value of elements to be replaced;
- newVal – value of the elements that replace the old values of oldVal.
Example.
#include <iostream> #include <list> #include <algorithm> #include <vector> #include <functional> #include <time.h> using namespace std; void main() { // The replace_copy algorithm - replaces characters in a sequence. // Creates a new result sequence. // 1. Create a list list<int> l; // Fill list with new values 2..5 for (int i = 0; i < 10; i++) l.push_back(rand() % (5 - 2 + 1) + 2); // 2. Display the list list<int>::iterator it = l.begin(); cout << "Old list: " << endl; while (it != l.end()) { cout << *it << " "; it++; } cout << endl; // 3. Demonstration of the replace_copy() function // 3.1. Create a new result list list<int> l2(l.size()); // 3.2. Call the replace_copy algorithm replace_copy(l.begin(), l.end(), l2.begin(), 2, 3); // replaces all numbers 2 with 3 // 4. Print the new list cout << "A new list:" << endl; it = l2.begin(); while (it != l2.end()) { cout << *it << " "; it++; } }
Result
Old list: 3 5 4 2 3 2 4 4 4 2 A new list: 3 5 4 3 3 3 4 4 4 3
⇑