Standard Template Library: Algorithms
Submitted by pavel7_7_7 on Monday, February 10, 2014 - 10:55.
In this article you will find a description of some algorithms from the Standard Template Library.
Standard Template Library provides large number of algorithms that can be performed on the data that is stored in containers. The use of these algorithms can increase the efficiency of your program and avoid a lot of mistakes.
The first step to use algorithms is to include algorithm header file:
I'll test all algorithm on a vector of integers. Now, I'll create a vector, that store the data:
Now we can print the data from the test vector:
Output:
The first example of use an algorithm is
The for_each function performs action on the range [first;last] according to the func function. For example, I create a function, called
And now I can call the
And the output is the same:
The next function is
There is another implementation of the find function - find_if. This functions finds an element from the container according to the unary pridicate. To use it you need to have an unary predicate. I'll try to find a negative element in the vector:
Now You can use this predicate in find_if function:
Next function is
The next function is
This code will replace first 3 elements of the test vector by integers from the ar array.
Another opportunity is to swap to elements. For example we have to variable and we need to swap the values of these variables:
Another useful function is
Also you can find the minimum and the maximum of two elements by using min and max function:
You can find the maximum and the minimum element in a container by using max_element and min_element:
These functions return iterators to the smallest and the biggest element;
You can reverse a range of elements by using
Of course, this is the very small description of the STL algorithms. But even these simple functions can make you programming life much more easier. You can download the source code of the examples shown in the article and try it by yourself.
- #include <algorithm>
- vector<int> test;
- test.push_back(5);
- test.push_back(12);
- test.push_back(3);
- test.push_back(5);
- test.push_back(1);
- test.push_back(-2);
- test.push_back(81);
- test.push_back(54);
- for(int i = 0; i != test.size();++i)
- cout << test[i] << " ";
for_each
function.
The definition of function is:
- for_each (InputIterator first, InputIterator last, Function func);
print
:
- int print(int i){
- cout << i << " ";
- return 0;
- }
for_each
function:
- cout << endl << "For each used:" <<endl;
- for_each(test.begin(),test.end(),print);
find
. This function returns iterator to the first element found in a range. We can found the any element and return it from the container:
- vector<int>::iterator it;
- it = find(test.begin(),test.end(),-2);
- cout << endl << "Element found " << *it << endl;
- bool isNegative(int i){
- return i < 0;
- }
- it = find_if(test.begin(),test.end(),isNegative);
- cout << endl << "Element found " << *it << endl;
count
. This function counts how many time an element is found in a range:
- cout << "The element 5 has " << count(test.begin(),test.end(),5)<< " appearences" << endl;
copy
. This functions copies a range of elements from source to destination:
- int ar[3] = {1,2,3};
- copy(ar,ar + 3, test.begin());
- cout << "Elements copied: " << endl;
- for_each(test.begin(),test.end(),print);
- int first = 1;
- int second = 2;
- swap(first,second);
- cout << "\nNow first is " << first <<" and second is " << second << endl;
sort
function. To sort a vector just call the sort function with the range that should be sorted:
- sort(test.begin(),test.end());
- cout << "Sorted vector: " << endl;
- for_each(test.begin(),test.end(),print);
- cout << "The minimum of 5 and 7 is " << min(5,7) << endl;
- cout << "The maximum of 12 and 4 is " << max(12,4) << endl;
- cout << "The minimum from the vector is " << *min_element(test.begin(),test.end()) <<endl;
- cout << " The maximum from the vector is " << *max_element(test.begin(),test.end());
reverse
function:
- reverse(test.begin(),test.end());
- cout << "Reverse vector: " << endl;
- for_each(test.begin(),test.end(),print);
Add new comment
- 8 views