Block 4 – Algorithms: Modifying Sequence Operations
Unlike non-modifying algorithms, these algorithms change the container’s contents or order.
Categories in this block:
- Generating and copying data
- Swapping and transforming elements
- Replacement and removal operations
- Sequence reordering algorithms
4.1 Generating and Copying Algorithms
These algorithms copy or generate values in containers.
std::copy
Purpose
Copies elements from one range to another.
Syntax
std::copy(source_begin, source_end, destination_begin);
Example
#include <vector>
#include <algorithm>
#include <iostream>int main()
{
std::vector<int> v1 = {1,2,3};
std::vector<int> v2(3); std::copy(v1.begin(), v1.end(), v2.begin()); for(int x : v2)
std::cout << x << " ";
}
Output
1 2 3
std::copy_backward
Purpose
Copies elements starting from the end of the range.
Useful when ranges overlap.
Example
std::vector<int> v = {1,2,3,4,5};std::copy_backward(v.begin(), v.begin()+3, v.end());
std::fill
Purpose
Assigns the same value to all elements in a range.
Example
std::vector<int> v(5);std::fill(v.begin(), v.end(), 10);
Result
10 10 10 10 10
std::fill_n
Purpose
Assigns a value to n elements.
Syntax
std::fill_n(iterator, count, value);
Example
std::vector<int> v(5);std::fill_n(v.begin(), 3, 7);
Result
7 7 7 0 0
std::generate
Purpose
Generates values using a function.
Example
int generateValue()
{
static int x = 1;
return x++;
}std::vector<int> v(5);std::generate(v.begin(), v.end(), generateValue);
Result
1 2 3 4 5
std::generate_n
Purpose
Generates n elements using a function.
Example
std::generate_n(v.begin(), 3, generateValue);
4.2 Swapping and Transformation Algorithms
These algorithms exchange values or transform data.
std::swap
Purpose
Swaps two variables.
Example
int a = 10;
int b = 20;std::swap(a, b);
Result
a = 20
b = 10
std::swap_ranges
Purpose
Swaps elements between two ranges.
Example
std::vector<int> v1 = {1,2,3};
std::vector<int> v2 = {4,5,6};std::swap_ranges(v1.begin(), v1.end(), v2.begin());
Result
v1 → 4 5 6
v2 → 1 2 3
std::iter_swap
Purpose
Swaps elements pointed to by iterators.
Example
std::vector<int> v = {1,2,3};std::iter_swap(v.begin(), v.begin()+2);
Result
3 2 1
std::transform
Purpose
Applies a function to elements and stores the result.
Syntax
std::transform(begin, end, destination, function);
Example
std::vector<int> v = {1,2,3};
std::vector<int> result(3);std::transform(v.begin(), v.end(), result.begin(),
[](int x){ return x * 2; });
Result
2 4 6
4.3 Replacement and Removal Algorithms
These algorithms replace values or remove elements.
std::replace
Purpose
Replaces all occurrences of a value.
Example
std::vector<int> v = {1,2,3,2};std::replace(v.begin(), v.end(), 2, 9);
Result
1 9 3 9
std::remove
Purpose
Moves elements to remove a value.
Important:
It does not actually erase elements from the container.
Example
std::vector<int> v = {1,2,3,2};auto it = std::remove(v.begin(), v.end(), 2);
To fully remove:
v.erase(it, v.end());
This is called the Erase-Remove Idiom.
std::remove_if
Purpose
Removes elements based on a condition.
Example
auto it = std::remove_if(v.begin(), v.end(),
[](int x){ return x > 3; });v.erase(it, v.end());
std::unique
Purpose
Removes consecutive duplicate elements.
Example
std::vector<int> v = {1,1,2,2,3};auto it = std::unique(v.begin(), v.end());
Result
1 2 3
std::unique_copy
Purpose
Copies unique elements to another container.
Example
std::vector<int> src = {1,1,2,2,3};
std::vector<int> dest(5);std::unique_copy(src.begin(), src.end(), dest.begin());
4.4 Sequence Reordering Algorithms
These algorithms change the order of elements.
std::reverse
Purpose
Reverses elements in a container.
Example
std::vector<int> v = {1,2,3,4};std::reverse(v.begin(), v.end());
Result
4 3 2 1
std::reverse_copy
Purpose
Copies reversed sequence to another container.
Example
std::vector<int> v = {1,2,3};
std::vector<int> r(3);std::reverse_copy(v.begin(), v.end(), r.begin());
Result
3 2 1
std::rotate
Purpose
Rotates elements around a pivot.
Example
std::vector<int> v = {1,2,3,4,5};std::rotate(v.begin(), v.begin()+2, v.end());
Result
3 4 5 1 2
std::partition
Purpose
Reorders elements based on condition.
Example
std::vector<int> v = {1,2,3,4,5};std::partition(v.begin(), v.end(),
[](int x){ return x % 2 == 0; });
Result
2 4 1 3 5
(Evens first)
std::stable_partition
Purpose
Same as partition but preserves order.
Example
std::stable_partition(v.begin(), v.end(),
[](int x){ return x % 2 == 0; });
Result
2 4 1 3 5
Order preserved within groups.
Summary Table
| Algorithm | Purpose |
|---|---|
| copy | copy elements |
| fill | assign value |
| generate | generate values |
| swap | exchange values |
| transform | modify elements |
| replace | replace values |
| remove | remove elements |
| unique | remove duplicates |
| reverse | reverse order |
| rotate | rotate elements |
| partition | separate elements by condition |
Key Exam Points
- Modifying algorithms change container content or order.
remove()does not erase elements, must useerase.unique()removes consecutive duplicates only.transform()applies a function to elements.partition()reorders elements based on a predicate.
✅ Block 4 Summary
Modifying algorithms allow you to:
- Generate data
- Copy sequences
- Transform values
- Remove duplicates
- Rearrange elements
They are powerful tools for efficient container manipulation.