πSTL in CPP ( Part VI )
STANDARD TEMPLATE LIBRARY (STL) IV - ALGORITHMS - 2023
Algorithms
The standard algorithms are found in <algorithms>. There are about 60 standard algorithms are defined in <algorithms>.
STL Algorithms Library can be characterized as following:
Sorting algorithms Quicksort algorithm over elements a to b:
Stable sort algorithm over elements a to b:
Note: "Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a sorting algorithm is stable if whenever there are two records R and S with the same key and with R appearing before S in the original list, R will appear before S in the sorted list." - wiki on stable sort
Non-mutating sequence algorithms It does not modify contents of the containers they work on Typical operation is searching container for particular element and returning its position.
Finds position of t in range a to b:
Finds position of first element that makes predicate true in range of a to b, otherwise position b returned:
Mutating sequence algorithms
STL's copy() function copies elements from a range to a location identified by an iterator:
A sample code is available at STL iterators - copy_algorithm.
Numerical algorithms
Sums and Inner products
Output:
Adjacent difference
Generally use iterators to access containers instantiated on a given type
An input sequence is defined by a pair of iterators; an output sequence is defined by an iterator to its first element. In general, an algorithm is parameterized by one or more operations that can be defined as functions or as function objects. By returning the end of an input sequence, the algorithms indicate failure. For instance, find(begin, end, x) returns end if it can not find x.
find()
find is one of the non-modifying sequence operations. It finds the first occurrence of a value in a range:
Output is:
find() operates on a sequence defined by pair of iterators. We're trying to find val in the range [fist:last). The find() returns iterator as the result. The iterator points either to the first element of the sequence with the value val or to last. Returning an iterator to the one-beyond-the-last element of a sequence indicates not found.
The sequence consists of all the elements of a container, in this case, vector:
The iterator operations used are those of a vector<int>::iterator, in other words, ++ simply moves a pointer to the next location in memory and * dereferences the pointer.
The iterator comparison, first != last is a pointer comparison, and the value comparison *first != val simply compares two integers.
We check the returned iterator against the end of our sequence to see if we found the value:
We could have written the implicit loop of the find() more explicitly:
Note that we needed addition variable iter. So, the original find() may be more efficient and more compact.
The find() algorithm we wrote is generic. In other words, it can be used for different data types.
Here is an example using list instead of vector with the same find():
Output is:
Here, the iteration for find() is that of a list<string>::iterator. The operators have the required meaning, so that the underlying logic remains the same as for the vector<int> of the previous example. However, the implementation is different. In other words, ++ of the ++first simply follows a pointer in the link (or next) part of the element to where the next element of the list is stored.
From the examples above, we can easily find that the find() is extremely flexible as long as we obey the simple rules for iterators.
find_if()
Typically, we don't look for a specific value, rather we are more often look for a value that meets a certain criteria. The find() would be more useful if we could set our own find criteria. The standard algorithm that searches based on a criterion of user's choice is find_if():
When we compare the find_if() with find(), it uses !predicate(*first) as a stop condition rather than *first != val. So, it stops searching once the predicate() succeeds rather than when an element has the same value with val.
A predicate is a function that returns true or false. Our find_if() needs a predicate that takes one argument so that it can say predicate(*first).
Here are the examples of predicates:
And the complete code of using the predicates and find_if() is:
Output from the run is:
The find_if() calls GreaterThan5() or FirstEven() for each element until it finds the right element.
Functors
In the previous example, we used a predicate:
That is ugly because if we want to find an element greater than 10, we have to make another one. So, we need to find a better way! Let's investigate what the property of GreaterThan() predicate. First, we can call it as a predicate like predicate(*first). Second, we can store a value, such as 10 or x so that it can be used when called.
That's the reason why we need a function object. In other words, we need an object that can behave like a function while it can store data. Here is the object we need:
When we say GreaterThan(5), we make an object of class GreaterThan holding 5 as its member data:
We pass that object to find_if() as its parameter called predicate. For each element of v, find_if() makes a call
This will invoke GreaterThan::operator() which is our function object using the argument *first. The result is a comparison of the element's value, *first, with 5.
What do we see here? The function call can be seen as an operator, the () operator, just like any operator. Therefore, () in predicate(*first) is given a meaning by GreaterThan::operator(), just as subscripting in v[i] is given a meaning by vector::operator[].
Here is the complete code:
Now, we established a way of allowing a function to carry around data that it needs. Obviously, the function objects provide us with a very powerful and convenient mechanism.
accumulate()
accumulate() is one of the simplest and most useful numerical algorithm:
It adds a sequence of values, and the type of the result a is the type of the initial value val. It is found in <numeric>.
Here is another accumulator() which has four arguments, with the last one, we can specify the operation to be used:
Output is:
Note that unlike other operations, the minus() operation was done against integers.
transform()
The op will be applied to all the elements in the input range ([first,last)) and stores each returned value in the range beginning at output.
Here is the sample which converts a string to uppercase.
List of algorithms
r=find(b,e,v)
r points to the first occurrence of v in [b:e).
r=find_if(b,e,v)
r points to the first occurrence of v in [b:e) so that v(x) is true.
r=count(b,e,v)
r is the number of occurrence of v in [b:e).
r=count_if(b,e,v)
r is the number of occurrence in [b:e) so that v(x) is true.
sort(b,e)
Sort [b,e) using <.
sort(b,e,v)
Sort [b,e) using v.
copy(b,e,b2)
Copy [b,e) to [b2:b2+(e-b)); there had better be enough elements after b2.
unique_copy(b,e,b2)
Copy [b,e) to [b2:b2+(e-b)); don't copy adjacent duplicates.
merge(b,e,b2,e2,r)
Merge two sorted sequencec [b2,e2) and [b,e) into [r:r+(e-b)+(e2-b2)).
r=equal_range(b,e,v)
r is te subsequence of the sorted range [b,e) with the value v, basically, a binary search for v.
equal(b,e,b2)
Do all elements of [b,e) and [b2:b2+(e-b)) compare equal?
x=accumulate(b,e,i)
x is the sum of i and the elements of [b,e).
x=accumulate(b,e,i,op)
Like the other accumulate, but with the sum calculate using op.
x=inner_product(b,e,b2,i)
x is the inner product of [b,e) and [b2:b2+(e-b)).
x=inner_product(b,e,b2,i,op,op2)
Like the othe inner_product, but with op and op2 instead of + and *.
Last updated