C++ Template Library Module36 Tenouk
C++ Template Library Module36 Tenouk
--THE STL--
ALGORITHM PART IV
Note: Compiled using Microsoft Visual C++ .Net, win32 empty console mode application. g++ compilation
examples given at the end of this Module.
Abilities
push_heap()
- Adds an element that is at the end of a range to an existing heap consisting of the prior elements in the
range.
template<class RandomAccessIterator>
void push_heap(
RandomAccessIterator _First,
RandomAccessIterator _Last
);
template<class RandomAccessIterator, class BinaryPredicate>
void push_heap(
RandomAccessIterator _First,
RandomAccessIterator _Last,
BinaryPredicate _Comp
);
Parameters
Parameter Description
_First A random-access iterator addressing the position of the first element in the heap.
A random-access iterator addressing the position one past the final element in the
_Last
range to be converted into a heap.
User-defined predicate function object that defines sense in which one element is
_Comp less than another. A binary predicate takes two arguments and returns true when
satisfied and false when not satisfied.
Table 36.1
- The element must first be pushed back to the end of an existing heap and then the algorithm is used to
add this element to the existing heap. Repeat again, heaps have two properties:
- Heaps are an ideal way to implement priority queues and they are used in the implementation of the
STL container adaptor priority_queue Class.
- The range referenced must be valid; all pointers must be de-referenceable and within the sequence the
last position is reachable from the first by incrementation.
- The range excluding the newly added element at the end must be a heap.
- The complexity is logarithmic, requiring at most log (_Last – _First) comparisons.
//algorithm, push_heap()
//make_heap()
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
www.tenouk.com Page 1 of 32
int main()
{
vector <int> vec1;
vector <int>::iterator Iter1;
int i;
for(i = 1; i <= 10; i++)
vec1.push_back(i);
random_shuffle(vec1.begin(), vec1.end());
cout<<"\npush_heap()...."<<endl;
push_heap(vec1.begin(), vec1.end());
cout<<"The default reheaped vec1 with data added is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
cout<<"\npush_back()..."<<endl;
vec1.push_back(0);
cout<<"The greater than heap vec1 with 13 pushed back is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
cout<<"\npush_heap()...."<<endl;
push_heap(vec1.begin(), vec1.end(), greater<int>());
cout<<"The greater than reheaped vec1 with 13 added is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
}
Output:
www.tenouk.com Page 2 of 32
random_shuffle()
template<class RandomAccessIterator>
void random_shuffle(
RandomAccessIterator _First,
RandomAccessIterator _Last
);
template<class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle(
RandomAccessIterator _First,
RandomAccessIterator _Last,
RandomNumberGenerator& _Rand
);
Parameters
Parameter Description
A random-access iterator addressing the position of the first element in the range to
_First
be rearranged.
A random-access iterator addressing the position one past the final element in the
_Last
range to be rearranged.
_Rand A special function object called a random number generator.
Table 36.2
- The two versions (refer to the templates) of the function differ in how they generate random numbers.
- The first version uses an internal random number generator and the second a random number generator
function object that is explicitly passed and can accept a seed value.
//algorithm, random_shuffle()
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
www.tenouk.com Page 3 of 32
vector <int>::iterator Iter1;
int i;
for(i = 1; i <= 10; i++)
vec1.push_back(i);
cout<<"The original of vector vec1 data:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
//random shuffle…
random_shuffle(vec1.begin(), vec1.end());
cout<<"The original of vector vec1 random shuffle data:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
//Shuffled once
random_shuffle(vec1.begin(), vec1.end());
push_heap(vec1.begin(), vec1.end());
cout<<"Vector vec1 after reshuffle is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
//Shuffled again
random_shuffle(vec1.begin(), vec1.end());
push_heap(vec1.begin(), vec1.end());
cout<<"Vector vec1 after another reshuffle is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
}
Output:
remove()
- Eliminates a specified value from a given range without disturbing the order of the remaining elements
and returning the end of a new range free of the specified value.
Parameters
Parameter Description
A forward iterator addressing the position of the first element in the range from
_First
which elements are being removed.
A forward iterator addressing the position one past the final element in the range
_Last
from which elements are being removed.
_Val The value that is to be removed from the range.
Table 36.3
www.tenouk.com Page 4 of 32
- The return value is a forward iterator addressing the new end position of the modified range, one past
the final element of the remnant sequence free of the specified value.
- The range referenced must be valid; all pointers must be de-referenceable and within the sequence the
last position is reachable from the first by incrementation.
- The order of the elements not removed remains stable.
- The operator== used to determine the equality between elements must impose an equivalence
relation between its operands.
- The complexity is linear; there are (_Last – _First) comparisons for equality.
- The list Class class has a more efficient member function version of remove(), which also re-links
pointers.
//algorithm, remove()
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator Iter1, new_end;
int i;
for(i = 1; i <= 10; i++)
vec1.push_back(i);
int j;
for(j = 0; j <= 2; j++)
vec1.push_back(8);
random_shuffle(vec1.begin(), vec1.end());
cout<<"Vector vec1 random shuffle data is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
Output:
remove_copy()
www.tenouk.com Page 5 of 32
- Copies elements from a source range to a destination range, except that elements of a specified value
are not copied, without disturbing the order of the remaining elements and returning the end of a new
destination range.
Parameters
Parameter Description
An input iterator addressing the position of the first element in the range from
_First
which elements are being removed.
An input iterator addressing the position one past the final element in the range
_Last
from which elements are being removed.
An output iterator addressing the position of the first element in the destination
_Result
range to which elements are being removed.
_Val The value that is to be removed from the range.
Table 36.4
- The return value is a forward iterator addressing the new end position of the destination range, one past
the final element of the copy of the remnant sequence free of the specified value.
- The source and destination ranges referenced must be valid; all pointers must be de-referenceable and
within the sequence the last position is reachable from the first by incrementation.
- There must be enough space in the destination range to contain the remnant elements that will be
copied after elements of the specified value are removed.
- The order of the elements not removed remains stable.
- The operator== used to determine the equality between elements must impose an equivalence
relation between its operands.
- The complexity is linear; there are (_Last – _First) comparisons for equality and at most
(_Last – _First) assignments.
//algorithm, remove_copy()
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2(10);
vector <int>::iterator Iter1, Iter2, new_end;
int i;
for(i = 0; i <= 10; i++)
vec1.push_back(i);
int j;
for(j = 0; j <= 2; j++)
vec1.push_back(5);
random_shuffle(vec1.begin(), vec1.end());
cout<<"The original random shuffle vector vec1 data:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
www.tenouk.com Page 6 of 32
cout<<"Vector vec1 is left unchanged as:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
Output:
remove_copy_if()
- Copies elements from a source range to a destination range, except that satisfying a predicate are not
copied, without disturbing the order of the remaining elements and returning the end of a new
destination range.
Parameters
Parameter Description
An input iterator addressing the position of the first element in the range
_First
from which elements are being removed.
An input iterator addressing the position one past the final element in the
_Last
range from which elements are being removed.
An output iterator addressing the position of the first element in the
_Result
destination range to which elements are being removed.
The unary predicate that must be satisfied is the value of an element is to be
_Pred
replaced.
Table 36.5
- The return value is a forward iterator addressing the new end position of the destination range, one past
the final element of the remnant sequence free of the elements satisfying the predicate.
- The source range referenced must be valid; all pointers must be de-referenceable and within the
sequence the last position is reachable from the first by incrementation.
- There must be enough space in the destination range to contain the remnant elements that will be
copied after elements of the specified value are removed.
- The order of the elements not removed remains stable.
- The operator== used to determine the equality between elements must impose an equivalence
relation between its operands.
- The complexity is linear: there are (_Last – _First) comparisons for equality and at most
(_Last – _First) assignments.
//algorithm, remove_copy_if()
#include <vector>
www.tenouk.com Page 7 of 32
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2(14);
vector <int>::iterator Iter1, Iter2, new_end;
int i;
for(i = 0; i <= 10; i++)
vec1.push_back(i);
int j;
for(j = 0; j <= 2; j++)
vec1.push_back(5);
random_shuffle(vec1.begin(), vec1.end());
cout<<"The original random shuffle vector vec1 data:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
Output:
remove_if()
- Eliminates elements that satisfy a predicate from a given range without disturbing the order of the
remaining elements and returning the end of a new range free of the specified value.
Parameters
www.tenouk.com Page 8 of 32
Parameter Description
A forward iterator pointing to the position of the first element in the range
_First
from which elements are being removed.
A forward iterator pointing to the position one past the final element in the
_Last
range from which elements are being removed.
The unary predicate that must be satisfied is the value of an element is to
_Pred
be replaced.
Table 36.6
- The return value is a forward iterator addressing the new end position of the modified range, one past
the final element of the remnant sequence free of the specified value.
- The range referenced must be valid; all pointers must be de-referenceable and within the sequence the
last position is reachable from the first by incrementation.
- The order of the elements not removed remains stable.
- The operator== used to determine the equality between elements must impose an equivalence
relation between its operands.
- The complexity is linear: there are (_Last – _First) comparisons for equality.
- List has a more efficient member function version of remove which re-links pointers.
//algorithm, remove_if()
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator Iter1, new_end;
int i;
for(i = 0; i <= 9; i++)
vec1.push_back(i);
int j;
for(j = 0; j <= 2; j++)
vec1.push_back(4);
random_shuffle(vec1.begin(), vec1.end());
cout<<"Random shuffle vector vec1 data is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
Output:
www.tenouk.com Page 9 of 32
replace()
Parameters
Parameter Description
A forward iterator pointing to the position of the first element in the range from
_First
which elements are being replaced.
A forward iterator pointing to the position one past the final element in the range
_Last
from which elements are being replaced.
_OldVal The old value of the elements being replaced.
_NewVal The new value being assigned to the elements with the old value.
Table 36.7
- The range referenced must be valid; all pointers must be de-referenceable and within the sequence the
last position is reachable from the first by incrementation.
- The order of the elements not replaced remains stable.
- The operator== used to determine the equality between elements must impose an equivalence
relation between its operands.
- The complexity is linear; there are (_Last – _First) comparisons for equality and at most
(_Last – _First) assignments of new values.
//algorithm, replace()
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator Iter1;
int i;
for(i = 0; i <= 9; i++)
vec1.push_back(i);
int j;
for(j = 0; j <= 2; j++)
vec1.push_back(5);
random_shuffle(vec1.begin(), vec1.end());
cout<<"The original random shuffle vector vec1 data is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
www.tenouk.com Page 10 of 32
replace (vec1.begin( ), vec1.end( ), 3, 23);
replace (vec1.begin( ), vec1.end( ), 7, 77);
replace (vec1.begin( ), vec1.end( ), 0, 21);
Output:
replace_copy()
- Examines each element in a source range and replaces it if it matches a specified value while copying
the result into a new destination range.
Parameters
Parameter Description
An input iterator pointing to the position of the first element in the range from which
_First
elements are being replaced.
An input iterator pointing to the position one past the final element in the range from
_Last
which elements are being replaced.
An output iterator pointing to the first element in the destination range to where the
_Result
altered sequence of elements is being copied.
_OldVal The old value of the elements being replaced.
_NewVal The new value being assigned to the elements with the old value.
Table 36.8
- The value return is an output iterator pointing to the position one past the final element in the
destination range to where the altered sequence of elements is being copied.
- The source and destination ranges referenced must not overlap and must both be valid: all pointers
must be de-referenceable and within the sequences the last position is reachable from the first by
incrementation.
- The order of the elements not replaced remains stable.
- The operator== used to determine the equality between elements must impose an equivalence
relation between its operands.
- The complexity is linear: there are (_Last – _First) comparisons for equality and at most
(_Last – _First) assignments of new values.
//algorithm, replace_copy()
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
www.tenouk.com Page 11 of 32
{
vector <int> vec1;
list <int> lst1 (15);
vector <int>::iterator Iter1;
list <int>::iterator lstIter;
int i;
for (i = 0; i <= 9; i++)
vec1.push_back(i);
int j;
for (j = 0; j <= 3; j++)
vec1.push_back(7);
random_shuffle(vec1.begin(), vec1.end());
int k;
for (k = 0; k <= 15; k++)
vec1.push_back(1);
cout<<"The original random shuffle vector vec1 with appended data is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
cout<<"The list copy lst1 of vec1 with the value 0 replacing the 7 is:\n";
for(lstIter = lst1.begin(); lstIter != lst1.end(); lstIter++)
cout<<*lstIter<<" ";
cout<<endl;
}
Output:
replace_copy_if()
- Examines each element in a source range and replaces it if it satisfies a specified predicate while
copying the result into a new destination range.
www.tenouk.com Page 12 of 32
Parameters
Parameter Description
An input iterator pointing to the position of the first element in the range
_First
from which elements are being replaced.
An input iterator pointing to the position one past the final element in the
_Last
range from which elements are being replaced.
An output iterator pointing to the position of the first element in the
_Result
destination range to which elements are being copied.
The unary predicate that must be satisfied is the value of an element is to
_Pred
be replaced.
The new value being assigned to the elements whose old value satisfies
_Val
the predicate.
Table 36.9
- The source and destination ranges referenced must not overlap and must both be valid: all pointers
must be de-referenceable and within the sequences the last position is reachable from the first by
incrementation.
- The order of the elements not replaced remains stable.
- The operator== used to determine the equality between elements must impose an equivalence
relation between its operands.
- The complexity is linear; there are (_Last – _First) comparisons for equality and at most
(_Last – _First) assignments of new values.
//algorithm, replace_copy_if()
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
list <int> lst1 (13);
vector <int>::iterator Iter1;
list <int>::iterator lstIter1;
int i;
for (i = 0; i <= 9; i++)
vec1.push_back(i);
int j;
for (j = 0; j <= 3; j++)
vec1.push_back(7);
random_shuffle(vec1.begin(), vec1.end());
int k;
for(k = 0; k <= 13; k++)
vec1.push_back(3);
cout<<"The original random shuffle vector vec1 data with appended data is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
www.tenouk.com Page 13 of 32
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
cout<<"A list copy of vector vec1 with the value -8\n replacing "
<<"those greater than 5 is:\n";
for(lstIter1 = lst1.begin(); lstIter1 != lst1.end(); lstIter1++)
cout<<*lstIter1<<" ";
cout<<endl;
}
Output:
replace_if()
Parameters
Parameter Description
A forward iterator pointing to the position of the first element in the range from
_First
which elements are being replaced.
An iterator pointing to the position one past the final element in the range from
_Last
which elements are being replaced.
The unary predicate that must be satisfied is the value of an element is to be
_Pred
replaced.
The new value being assigned to the elements whose old value satisfies the
_Val
predicate.
Table 36.10
- The range referenced must be valid; all pointers must be de-referenceable and within the sequence the
last position is reachable from the first by incrementation.
- The order of the elements not replaced remains stable.
- The algorithm replace_if() is a generalization of the algorithm replace(), allowing any
predicate to be specified, rather than equality to a specified constant value.
- The operator== used to determine the equality between elements must impose an equivalence
relation between its operands.
- The complexity is linear: there are (_Last – _First) comparisons for equality and at most
(_Last – _First) assignments of new values.
//algorithm, replace_if()
www.tenouk.com Page 14 of 32
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator Iter1;
int i;
for (i = 1; i <= 10; i++)
vec1.push_back(i);
int j;
for (j = 0; j <= 2; j++)
vec1.push_back(8);
random_shuffle(vec1.begin(), vec1.end());
cout<<"The original random shuffle vector vec1 data is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
Output:
reverse()
template<class BidirectionalIterator>
void reverse(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
Parameters
Parameter Description
A bidirectional iterator pointing to the position of the first element in the range
_First
within which the elements are being permuted.
A bidirectional iterator pointing to the position one past the final element in the
_Last
range within which the elements are being permuted.
Table 36.11
- The source range referenced must be valid; all pointers must be de-referenceable and within the
sequence the last position is reachable from the first by incrementation.
www.tenouk.com Page 15 of 32
//algorithm, reverse()
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator Iter1;
int i;
for(i = 11; i <= 20; i++)
vec1.push_back(i);
Output:
reverse_copy()
- Reverses the order of the elements within a source range while copying them into a destination range
Parameters
Parameter Description
A bidirectional iterator pointing to the position of the first element in the source
_First
range within which the elements are being permuted.
A bidirectional iterator pointing to the position one past the final element in the
_Last
source range within which the elements are being permuted.
An output iterator pointing to the position of the first element in the destination
_Result
range to which elements are being copied.
Table 36.12
- The return value is an output iterator pointing to the position one past the final element in the
destination range to where the altered sequence of elements is being copied.
- The source and destination ranges referenced must be valid; all pointers must be de-referenceable and
within the sequence the last position is reachable from the first by incrementation.
//algorithm, reverse_copy()
#include <vector>
#include <algorithm>
www.tenouk.com Page 16 of 32
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2(11);
vector <int>::iterator Iter1, Iter2;
int i;
for(i = 10; i <= 20; i++)
vec1.push_back(i);
Output:
rotate()
template<class ForwardIterator>
void rotate(
ForwardIterator _First,
ForwardIterator _Middle,
ForwardIterator _Last
);
Parameters
Parameter Description
A forward iterator addressing the position of the first element in the range to be
_First
rotated.
A forward iterator defining the boundary within the range that addresses the position
_Middle of the first element in the second part of the range whose elements are to be
exchanged with those in the first part of the range.
A forward iterator addressing the position one past the final element in the range to
_Last
be rotated.
Table 36.13
- The ranges referenced must be valid; all pointers must be de-referenceable and within the sequence the
last position is reachable from the first by incrementation.
- The complexity is linear with at most (_Last – _First) swaps.
www.tenouk.com Page 17 of 32
//algorithm, rotate()
#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
deque <int> deq1;
vector <int>::iterator vec1Iter1;
deque<int>::iterator deq1Iter1;
int i;
for(i = -4; i <= 4; i++)
vec1.push_back(i);
int j;
for(j = -3; j <= 3; j++)
deq1.push_back(j);
//Let rotates...
rotate(vec1.begin(), vec1.begin() + 3, vec1.end());
cout<<"After rotating, vector vec1 data is: ";
for(vec1Iter1 = vec1.begin(); vec1Iter1 != vec1.end(); vec1Iter1++)
cout<<*vec1Iter1<<" ";
cout<<endl;
//Let rotates…
int k = 1;
while(k <= deq1.end() - deq1.begin())
{
rotate(deq1.begin(), deq1.begin() + 1, deq1.end());
cout<<"Rotation of a single deque element to the back,\n deq1 is: ";
for(deq1Iter1 = deq1.begin(); deq1Iter1 != deq1.end(); deq1Iter1++)
cout<<*deq1Iter1<<" ";
cout<<endl;
k++;
}
}
Output:
www.tenouk.com Page 18 of 32
rotate_copy()
- Exchanges the elements in two adjacent ranges within a source range and copies the result to a
destination range.
Parameters
Parameter Description
A forward iterator addressing the position of the first element in the range to be
_First
rotated.
A forward iterator defining the boundary within the range that addresses the position
_Middle of the first element in the second part of the range whose elements are to be
exchanged with those in the first part of the range.
A forward iterator addressing the position one past the final element in the range to be
_Last
rotated.
_Result An output iterator addressing the position of the first element in the destination range.
Table 36.14
- The return value is an output iterator addressing the position one past the final element in the
destination range.
- The ranges referenced must be valid; all pointers must be dereferenceable and within the sequence the
last position is reachable from the first by incrementation.
- The complexity is linear with at most (_Last – _First) swaps.
//algorithm, rotate_copy()
#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2(9);
deque <int> deq1, deq2(6);
vector <int>::iterator vec1Iter, vec2Iter;
deque<int>::iterator deq1Iter, deq2Iter;
int i;
for(i = -3; i <= 5; i++)
vec1.push_back(i);
int j;
for(j =0; j <= 5; j++)
deq1.push_back(j);
www.tenouk.com Page 19 of 32
cout<<"\nThe original deque deq1 is: ";
for(deq1Iter = deq1.begin(); deq1Iter != deq1.end(); deq1Iter++)
cout<<*deq1Iter<<" ";
cout<<endl;
int k = 1;
while(k <= deq1.end() - deq1.begin())
{
rotate_copy(deq1.begin(), deq1.begin() + 1, deq1.end(), deq2.begin());
cout<<"Rotation of a single deque element to the back,\n a deque copy, deq2 is: ";
for(deq2Iter = deq2.begin(); deq2Iter != deq2.end(); deq2Iter++)
cout<<*deq2Iter<<" ";
cout<<endl;
k++;
}
}
Output:
search()
- Searches for the first occurrence of a sequence within a target range whose elements are equal to those
in a given sequence of elements or whose elements are equivalent in a sense specified by a binary
predicate to the elements in the given sequence.
Parameters
Parameter Description
A forward iterator addressing the position of the first element in the range to
_First1
be searched.
A forward iterator addressing the position one past the final element in the
_Last1
range to be searched.
www.tenouk.com Page 20 of 32
A forward iterator addressing the position of the first element in the range to
_First2
be matched.
A forward iterator addressing the position one past the final element in the
_Last2
range to be matched.
User-defined predicate function object that defines the condition to be satisfied
_Comp if two elements are to be taken as equivalent. A binary predicate takes two
arguments and returns true when satisfied and false when not satisfied.
Table 36.15
- The return value is a forward iterator addressing the position of the first element of the first
subsequence that matches the specified sequence or that is equivalent in a sense specified by a binary
predicate.
- The operator== used to determine the match between an element and the specified value must
impose an equivalence relation between its operands.
- The ranges referenced must be valid; all pointers must be de-referenceable and within each sequence
the last position is reachable from the first by incrementation.
- Average complexity is linear with respect to the size of the searched range, and worst case complexity
is also linear with respect to the size of the sequence being searched for.
//algorithm, search()
//compiled with some type conversion
//warning…
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2;
list <int> lst1;
vector <int>::iterator Iter1, Iter2;
list <int>::iterator lst1_Iter, lst1_inIter;
int i;
for(i = 0; i <= 5; i++)
vec1.push_back(5*i);
int j;
for(j = 4; j <= 5; j++)
lst1.push_back(5*j);
int k;
for(k = 2; k <= 4; k++)
vec2.push_back(10*k);
www.tenouk.com Page 21 of 32
if(result1 == vec1.end())
cout<<"There is no match of lst1 in vec1."<<endl;
else
cout<<"There is at least one match of lst1 in vec1"
<<"\nand the first one begins at "
<<"position "<< result1 - vec1.begin( )<<endl;
//Searching vec1 for a match to lst1 under the binary predicate twice
vector <int>::iterator result2;
result2 = search(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice);
if(result2 == vec1.end())
cout<<"\nThere is no match of lst1 in vec1."<<endl;
else
cout<<"\nThere is a sequence of elements in vec1 that "
<<"are equivalent\nto those in vec2 under the binary "
<<"predicate twice\nand the first one begins at position "
<<result2 - vec1.begin()<<endl;
}
Output:
search_n()
- Searches for the first subsequence in a range that of a specified number of elements having a particular
value or a relation to that value as specified by a binary predicate.
Parameters
Parameter Description
A forward iterator addressing the position of the first element in the range to be
_First1
searched.
A forward iterator addressing the position one past the final element in the range to
_Last1
be searched.
_Count The size of the subsequence being searched for.
_Val The value of the elements in the sequence being searched for.
User-defined predicate function object that defines the condition to be satisfied if
_Comp two elements are to be taken as equivalent. A binary predicate takes two
arguments and returns true when satisfied and false when not satisfied.
Table 36.16
www.tenouk.com Page 22 of 32
- The return value is a forward iterator addressing the position of the first element of the first
subsequence that matches the specified sequence or that is equivalent in a sense specified by a binary
predicate.
- The operator== used to determine the match between an element and the specified value must
impose an equivalence relation between its operands.
- The range referenced must be valid; all pointers must be de-referenceable and within the sequence the
last position is reachable from the first by incrementation.
- Complexity is linear with respect to the size of the searched.
//algorithm, search_n()
//some type conversion warning
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2;
vector <int>::iterator Iter1;
int i;
for(i = 0; i <= 5; i++)
vec1.push_back(5*i);
if(result1 == vec1.end())
cout<<"\nThere is no match for a sequence (5 5 5) in vec1."<<endl;
else
cout<<"\nThere is at least one match of a sequence (5 5 5)"
<<"\nin vec1 and the first one begins at "
<<"position "<<result1 - vec1.begin()<<endl;
if(result2 == vec1.end())
cout<<"\nThere is no match for a sequence (5 5 5) in vec1"
<<" under the equivalence predicate twice."<<endl;
else
cout<<"\nThere is a match of a sequence (5 5 5) "
<<"under the equivalence\npredicate twice"
<<" in vec1 and the first one begins at "
<<"position "<<result1 - vec1.begin()<<endl;
}
Output:
www.tenouk.com Page 23 of 32
set_difference()
- Unites all of the elements that belong to one sorted source range, but not to a second sorted source
range, into a single, sorted destination range, where the ordering criterion may be specified by a binary
predicate.
Parameters
Parameter Description
An input iterator addressing the position of the first element in the first
_First1 of two sorted source ranges to be united and sorted into a single range
representing the difference of the two source ranges.
An input iterator addressing the position one past the last element in the
_Last1 first of two sorted source ranges to be united and sorted into a single
range representing the difference of the two source ranges.
An input iterator addressing the position of the first element in second
_First2 of two consecutive sorted source ranges to be united and sorted into a
single range representing the difference of the two source ranges.
An input iterator addressing the position one past the last element in
_Last2 second of two consecutive sorted source ranges to be united and sorted
into a single range representing the difference of the two source ranges.
An output iterator addressing the position of the first element in the
_Result destination range where the two source ranges are to be united into a
single sorted range representing the difference of the two source
ranges.
User-defined predicate function object that defines the sense in which
one element is greater than another. The binary predicate takes two
_Comp
arguments and should return true when the first element is less than the
second element and false otherwise.
Table 36.17
- The return value is an output iterator addressing the position one past the last element in the sorted
destination range representing the difference of the two source ranges.
- The sorted source ranges referenced must be valid; all pointers must be de-referenceable and within
each sequence the last position must be reachable from the first by incrementation.
www.tenouk.com Page 24 of 32
- The destination range should not overlap either of the source ranges and should be large enough to
contain the destination range.
- The sorted source ranges must each be arranged as a precondition to the application of the
set_difference() algorithm in accordance with the same ordering as is to be used by the
algorithm to sort the combined ranges.
- The operation is stable as the relative order of elements within each range is preserved in the
destination range. The source ranges are not modified by the algorithm merge.
- The value types of the input iterators need be less-than-comparable to be ordered, so that, given two
elements, it may be determined either that they are equivalent, in the sense that neither is less than the
other or that one is less than the other. This results in an ordering between the nonequivalent elements.
- When there are equivalent elements in both source ranges, the elements in the first range precede the
elements from the second source range in the destination range.
- If the source ranges contain duplicates of an element such that there are more in the first source range
than in the second, then the destination range will contain the number by which the occurrences of
those elements in the first source range exceed the occurrences of those elements in the second source
range.
- The complexity of the algorithm is linear with at most 2*((_Last1 – _First1)–(_Last2 –
_First2))–1 comparisons for nonempty source ranges.
//algorithm, set_difference()
#include <vector>
#include <algorithm>
//For greater<int>()
#include <functional>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1a, vec1b, vec1(12);
vector <int>::iterator Iter1a, Iter1b, Iter1, Result1;
int j;
for(j =-2; j <= 1; j++)
vec1b.push_back(j);
www.tenouk.com Page 25 of 32
cout<<"\nOriginal vector vec2b with range sorted by the\n"
<<"binary predicate greater is: ";
for(Iter2b = vec2b.begin(); Iter2b != vec2b.end(); Iter2b++)
cout<<*Iter2b<<" ";
cout<<endl;
Output
www.tenouk.com Page 26 of 32
set_intersection()
- Unites all of the elements that belong to both sorted source ranges into a single, sorted destination
range, where the ordering criterion may be specified by a binary predicate.
Parameters
Parameter Description
An input iterator addressing the position of the first element in the first of two
_First1 sorted source ranges to be united and sorted into a single range representing the
intersection of the two source ranges.
An input iterator addressing the position one past the last element in the first of
_Last1 two sorted source ranges to be united and sorted into a single range representing
the intersection of the two source ranges.
An input iterator addressing the position of the first element in second of two
_First2 consecutive sorted source ranges to be united and sorted into a single range
representing the intersection of the two source ranges.
An input iterator addressing the position one past the last element in second of
_Last2 two consecutive sorted source ranges to be united and sorted into a single range
representing the intersection of the two source ranges.
_Result An output iterator addressing the position of the first element in the destination
range where the two source ranges are to be united into a single sorted range
www.tenouk.com Page 27 of 32
representing the intersection of the two source ranges.
User-defined predicate function object that defines the sense in which one
element is greater than another. The binary predicate takes two arguments and
_Comp
should return true when the first element is less than the second element and false
otherwise.
Table 36.18
- The return value is an output iterator addressing the position one past the last element in the sorted
destination range representing the intersection of the two source ranges.
- The sorted source ranges referenced must be valid; all pointers must be dereferenceable and within
each sequence the last position must be reachable from the first by incrementation.
- The destination range should not overlap either of the source ranges and should be large enough to
contain the destination range.
- The sorted source ranges must each be arranged as a precondition to the application of the merge
algorithm in accordance with the same ordering as is to be used by the algorithm to sort the combined
ranges.
- The operation is stable as the relative order of elements within each range is preserved in the
destination range. The source ranges are not modified by the algorithm.
- The value types of the input iterators need be less-than comparable to be ordered, so that, given two
elements, it may be determined either that they are equivalent (in the sense that neither is less than the
other) or that one is less than the other.
- This results in an ordering between the nonequivalent elements.
- When there are equivalent elements in both source ranges, the elements in the first range precede the
elements from the second source range in the destination range.
- If the source ranges contain duplicates of an element, then the destination range will contain the
maximum number of those elements that occur in both source ranges.
- The complexity of the algorithm is linear with at most 2*((_Last1 – _First1)–(_Last2 –
_First2))–1 comparisons for nonempty source ranges.
//algorithm, set_intersection()
#include <vector>
#include <algorithm>
//For greater<int>()
#include <functional>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1a, vec1b, vec1(12);
vector <int>::iterator Iter1a, Iter1b, Iter1, Result1;
//Constructing vectors vec1a & vec1b with default less than ordering
int i;
for(i = -2; i <= 2; i++)
vec1a.push_back(i);
int j;
for(j = -4; j <= 0; j++)
vec1b.push_back(j);
www.tenouk.com Page 28 of 32
cout<<endl;
Output:
www.tenouk.com Page 29 of 32
- Program example compiled using g++.
//******algorandshuffle.cpp********
//algorithm, random_shuffle()
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1;
vector <int>::iterator Iter1;
int i;
for(i = 1; i <= 10; i++)
vec1.push_back(i);
cout<<"The original of vector vec1 data:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
//random shuffle…
random_shuffle(vec1.begin(), vec1.end());
cout<<"The original of vector vec1 random shuffle data:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
//Shuffled once
random_shuffle(vec1.begin(), vec1.end());
push_heap(vec1.begin(), vec1.end());
cout<<"Vector vec1 after reshuffle is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
//Shuffled again
random_shuffle(vec1.begin(), vec1.end());
push_heap(vec1.begin(), vec1.end());
cout<<"Vector vec1 after another reshuffle is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
}
www.tenouk.com Page 30 of 32
[bodo@bakawali ~]$ g++ algorandshuffle.cpp -o algorandshuffle
[bodo@bakawali ~]$ ./algorandshuffle
//*******algosearchn.cpp*********
//algorithm, search_n()
//some type conversion warning
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2;
vector <int>::iterator Iter1;
int i;
for(i = 0; i <= 5; i++)
vec1.push_back(5*i);
if(result1 == vec1.end())
cout<<"\nThere is no match for a sequence (5 5 5) in vec1."<<endl;
else
cout<<"\nThere is at least one match of a sequence (5 5 5)"
<<"\nin vec1 and the first one begins at "
<<"position "<<result1 - vec1.begin()<<endl;
if(result2 == vec1.end())
cout<<"\nThere is no match for a sequence (5 5 5) in vec1"
<<" under the equivalence predicate twice."<<endl;
else
cout<<"\nThere is a match of a sequence (5 5 5) "
<<"under the equivalence\npredicate twice"
<<" in vec1 and the first one begins at "
<<"position "<<result1 - vec1.begin()<<endl;
}
www.tenouk.com Page 31 of 32
Vector vec1 data: 0 5 10 15 20 25 5 5 5 0 5 10 15 20 25 5 5 5
www.tenouk.com Page 32 of 32