Standard Template Library: Algorithms

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:
  1. #include <algorithm>
I'll test all algorithm on a vector of integers. Now, I'll create a vector, that store the data:
  1. vector<int> test;
  2. test.push_back(5);
  3. test.push_back(12);
  4. test.push_back(3);
  5. test.push_back(5);
  6. test.push_back(1);
  7. test.push_back(-2);
  8. test.push_back(81);
  9. test.push_back(54);
Now we can print the data from the test vector:
  1. for(int i = 0; i != test.size();++i)
  2.         cout << test[i] << " ";
Output: The first example of use an algorithm is for_each function. The definition of function is:
  1. for_each (InputIterator first, InputIterator last, Function func);
The for_each function performs action on the range [first;last] according to the func function. For example, I create a function, called print:
  1. int print(int i){
  2.     cout << i << " ";
  3.     return 0;
  4. }
And now I can call the for_each function:
  1. cout << endl << "For each used:" <<endl;
  2. for_each(test.begin(),test.end(),print);
And the output is the same: The next function is 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:
  1. vector<int>::iterator it;
  2. it = find(test.begin(),test.end(),-2);
  3. cout << endl << "Element found " << *it << endl;
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:
  1. bool isNegative(int i){
  2.     return i < 0;
  3. }
Now You can use this predicate in find_if function:
  1. it = find_if(test.begin(),test.end(),isNegative);
  2. cout << endl << "Element found " << *it << endl;
Next function is count. This function counts how many time an element is found in a range:
  1.  cout << "The element 5 has " << count(test.begin(),test.end(),5)<< " appearences" << endl;
The next function is  copy . This functions copies a range of elements from source to destination:
  1. int ar[3] = {1,2,3};
  2. copy(ar,ar + 3, test.begin());
  3. cout << "Elements copied: " << endl;
  4. for_each(test.begin(),test.end(),print);
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:
  1. int first = 1;
  2. int second = 2;
  3. swap(first,second);
  4. cout << "\nNow first is " << first <<" and second is " << second << endl;
Another useful function is  sort function. To sort a vector just call the sort function with the range that should be sorted:
  1. sort(test.begin(),test.end());
  2. cout << "Sorted vector: " << endl;
  3.  for_each(test.begin(),test.end(),print);
Also you can find the minimum and the maximum of two elements by using min and max function:
  1. cout << "The minimum of 5 and 7 is " << min(5,7) << endl;
  2. cout << "The maximum of 12 and 4 is " << max(12,4) << endl;
You can find the maximum and the minimum element in a container by using max_element and min_element:
  1. cout << "The minimum from the vector is " << *min_element(test.begin(),test.end()) <<endl;
  2.     cout << " The maximum from the vector is " << *max_element(test.begin(),test.end());
These functions return iterators to the smallest and the biggest element; You can reverse a range of elements by using reverse function:
  1. reverse(test.begin(),test.end());
  2. cout << "Reverse vector: " << endl;
  3. for_each(test.begin(),test.end(),print);
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.

Add new comment