CS212: Object Oriented Analysis
and Design
STL Algorithms
Introduction
Problem statement: Suppose that we want to write a program
that reads in a list of integers from a file (data.txt), then prints
out the average of those values
ifstream
input("data.txt");
multiset<int>
values;
Read the
content of
the file
Add the
values
together
Divide by
number of
elements
double total = 0.0;
int currValue;
for
(multiset<int>::iterator
= values.begin();
cout << "Average
" <<>>total
/ itr
values.size()
<< endl;
while is:
(input
currValue)
itr != values.end(); ++itr)
values.insert(currValue);
total += *itr;
Discussion
Intent
In practice
“read the contents of the file
into the multiset“
“create an integer, and then
while it's possible to read
another element out of the file,
do so and insert it into the
multiset.”
“sum the elements together”
“initialize the total to zero,
then iterate over the elements
of the multiset, increasing the
total by the value of the
current element at each step.”
“Mechanical model”
Accumulate
Averaging program
double total = 0.0;
for (multiset::iterator itr = values.begin();
itr != values.end(); ++itr)
total += *itr;
cout << "Average is: " << total / values.size()
<< endl;
Using “accumulate” algorithm
cout <<
accumulate(values.begin(), values.end(), 0.0) /
values.size()
<< endl;
More on “accumulate”
• Defined in the <numeric> header
template <class InputIterator, class T>
T accumulate (InputIterator first, InputIterator last, T init);
• Three parameters (arguments) are passed to it
1.
2.
3.
Iterator 1: Beginning of the range of values
Iterator 2: End of the range of values
Initial value to be used in the summation
• Demonstration (AccumSum.cpp)
Do we really need them?
• There are reasons behind using STL algorithms
Simplicity
Correctness
Speed
Clarity
Reduces the
chance of slip
up and
making
mistakes
Optimized to
work as fast
as possible
Ease of
understanding
of existing
modules
Leverage
existing codes
A great timesaver
Reordering algorithms
• Reorder but preserve the elements in a container
• sort(RandomAccessIterator first,
RandomAccessIterator last );
• random_shuffle(RandomAccessIterator first,
RandomAccessIterator last));
• rotate(ForwardIterator first, ForwardIterator
middle, ForwardIterator last);
• Demonstration (ReorderAlgo.cpp)
Searching Algorithms
• Checking membership in a container
InputIterator find (InputIterator first,
InputIterator last, const T& val);
• Returns an iterator to the first element in the range that compares
equal to val
• If no such element is found, the function returns last.
bool binary_search (ForwardIterator first,
ForwardIterator last, const T& val);
• The elements in the range shall already be sorted
• Demonstration (SearchAlgo.cpp)
Removal Algorithms
• For removing elements from containers
• Removal algorithms do not actually remove elements from
containers
• Algorithms accept iterators, not containers
• Do not know how to erase elements from containers
• Removal functions work by shuffling down the contents of the
container
Removal algorithm: example
0
1
2
3
3
3
4
ForwardIterator remove (ForwardIterator first,
ForwardIterator last, const T& val);
The removal is done by replacing the elements that compare equal
to val by the next element that does not
F
L
0
1
2
34
3
3
4
==
==
==
==
==
==
==
3
3
3
3
3
3
3
Demonstration (RemoveAlgo.cpp)
© Copyright 2026 Paperzz