Problem solving with programming: C++ STL Algorithms - Sort - Part-2
In the last post, the basic use of STL sort() method is explained. In this post, I will discuss some more options available with this method.
We have seen in the previous post that sort takes the beginning and ending of the array as arguments.
sort( array.begin(), array.end() );
In addition to those parameters, it can also take a third parameter (predicate) which decides the ordering of the elements. If we do not pass the third parameter, the default ordering is assumed. For integer data types such as int, float, char etc, ascending order is followed. For strings alphabetical order is followed. To sort an array of integers in descending order, simply call the sort routine like this.
sort( array.begin(), array.end(), greater<int>() );
Read full article from Problem solving with programming: C++ STL Algorithms - Sort - Part-2
No comments:
Post a Comment