Algorithms: Non-Modifying Sequence Operations
Block 3 – Algorithms: Non-Modifying Sequence Operations
Non-modifying algorithms read elements but do not change them.
Typical uses:
- Iterating through containers
- Searching elements
- Counting elements
- Comparing ranges
These algorithms work with iterators rather than containers directly.
General syntax:
algorithm_name(begin_iterator, end_iterator, optional_parameters);
Example:
std::find(v.begin(), v.end(), 10);
3.1 Purpose of Non-Modifying Algorithms
Non-modifying algorithms:
- Access container elements
- Perform operations like searching or counting
- Do not alter container content
Advantages
✔ Code reuse
✔ Container independent
✔ Efficient implementations
✔ Work with all STL containers
Example:
std::vector<int> v = {1,2,3,4};auto it = std::find(v.begin(), v.end(), 3);
The vector remains unchanged.
3.2 Using std::for_each
Purpose
Applies a function to every element in a range.
Syntax
std::for_each(begin, end, function);
Example
#include <iostream>
#include <vector>
#include <algorithm>void print(int x)
{
std::cout << x << " ";
}int main()
{
std::vector<int> v = {1,2,3,4}; std::for_each(v.begin(), v.end(), print);
}
Output
1 2 3 4
Using Lambda
std::for_each(v.begin(), v.end(),
[](int x){ std::cout << x << " "; });
Use Cases
- Printing elements
- Updating values
- Logging operations
- Processing elements
3.3 Searching Algorithms
Searching algorithms locate elements in a range.
std::find
Searches for a specific value.
auto it = std::find(v.begin(), v.end(), 20);
Example
std::vector<int> v = {10,20,30};auto it = std::find(v.begin(), v.end(), 20);if(it != v.end())
std::cout << "Found";
std::find_if
Searches using a condition.
auto it = std::find_if(v.begin(), v.end(),
[](int x){ return x > 10; });
Example
std::vector<int> v = {5,8,12,3};auto it = std::find_if(v.begin(), v.end(),
[](int x){ return x > 10; });
Result → 12
std::find_end
Finds the last occurrence of a subsequence.
Example
std::vector<int> v = {1,2,3,1,2,3};
std::vector<int> sub = {1,2};auto it = std::find_end(v.begin(), v.end(),
sub.begin(), sub.end());
Result → second {1,2}.
std::find_first_of
Finds the first element from another range.
Example
std::vector<int> v = {5,6,7,8};
std::vector<int> targets = {7,9};auto it = std::find_first_of(v.begin(), v.end(),
targets.begin(), targets.end());
Result → 7.
std::adjacent_find
Finds two equal consecutive elements.
Example
std::vector<int> v = {1,2,2,3};auto it = std::adjacent_find(v.begin(), v.end());
Result → first 2.
std::search
Searches for a subsequence inside another sequence.
Example
std::vector<int> v = {1,2,3,4,5};
std::vector<int> pattern = {3,4};auto it = std::search(v.begin(), v.end(),
pattern.begin(), pattern.end());
Result → element 3.
std::search_n
Finds n consecutive occurrences of a value.
Example
std::vector<int> v = {1,2,2,2,3};auto it = std::search_n(v.begin(), v.end(), 3, 2);
Result → first 2.
3.4 Counting Algorithms
Counting algorithms count elements satisfying conditions.
std::count
Counts occurrences of a value.
Example
std::vector<int> v = {1,2,2,3};int c = std::count(v.begin(), v.end(), 2);
Result
2
std::count_if
Counts elements satisfying a condition.
Example
int c = std::count_if(v.begin(), v.end(),
[](int x){ return x > 2; });
Example program
std::vector<int> v = {1,5,10,15};int result = std::count_if(v.begin(), v.end(),
[](int x){ return x > 5; });
Result → 2.
3.5 Comparison Algorithms
Comparison algorithms compare two ranges of elements.
std::mismatch
Finds the first position where two ranges differ.
Example
std::vector<int> v1 = {1,2,3,4};
std::vector<int> v2 = {1,2,0,4};auto result = std::mismatch(v1.begin(), v1.end(),
v2.begin());
Result
3 vs 0
std::equal
Checks if two ranges contain identical elements.
Example
std::vector<int> a = {1,2,3};
std::vector<int> b = {1,2,3};bool same = std::equal(a.begin(), a.end(),
b.begin());
Result
true
Time Complexity
Most non-modifying algorithms run in:
O(n)
because they may need to check every element.
Summary Table
| Algorithm | Purpose |
|---|---|
for_each | Apply operation to all elements |
find | Find specific value |
find_if | Find element matching condition |
find_end | Find last occurrence of subsequence |
find_first_of | Find first element from another range |
adjacent_find | Find consecutive equal elements |
search | Find subsequence |
search_n | Find repeated elements |
count | Count occurrences |
count_if | Count elements satisfying condition |
mismatch | Find first difference between ranges |
equal | Check if two ranges are identical |
Key Exam Points
- Non-modifying algorithms do not change container data.
- They operate on iterator ranges.
- Most searching algorithms return an iterator.
countandcount_ifreturn numeric results.equalreturns true or false.mismatchreturns a pair of iterators.
✅ Block 3 Summary
Non-modifying algorithms are used for:
- Iterating elements
- Searching elements
- Counting elements
- Comparing ranges
They provide efficient, reusable operations independent of container type.