I,Sort: Sorts all elements in a certain range.
Prototype:
Template <class randomaccessiterator>
Void sort (randomaccessiterator first, randomaccessiterator last );
Template <class randomaccessiterator, class compare>
Void sort (randomaccessiterator first, randomaccessiterator last, compare comp );
Sample Code:
// Sort algorithm example
# Include <iostream>
# Include <algorithm>
# Include <vector>
Using namespace STD;
Bool myfunction (int I, Int J) {return (I <j );}
Struct myclass {
Bool operator () (int I, Int J) {return (I <j );}
} Myobject;
Int main (){
Int myints [] = };
Vector <int> myvector (myints, myints + 8); // 32 71 12 45 26 80 53 33
Vector <int>: iterator it;
// Using default comparison (operator <):
Sort (myvector. Begin (), myvector. Begin () + 4); // (12 32 45 71) 26 80 53 33
// Using function as comp
Sort (myvector. Begin () + 4, myvector. End (), myfunction); // 12 32 45 71 (26 33 53 80)
// Using object as comp
Sort (myvector. Begin (), myvector. End (), myobject); // (12 26 32 33 45 53 71 80)
// Print out content:
Cout <"myvector contains :";
For (IT = myvector. Begin (); it! = Myvector. End (); ++ it)
Cout <"" <* it;
Cout <Endl;
Return 0;
}
II,Stable_sort: Merge Sorting
Prototype:
Template <class randomaccessiterator>
Void stable_sort (randomaccessiterator first, randomaccessiterator last );
Template <class randomaccessiterator, class compare>
Void stable_sort (randomaccessiterator first, randomaccessiterator last,
Compare comp );
Sample Code:
// Stable_sort example
# Include <iostream>
# Include <algorithm>
# Include <vector>
Using namespace STD;
Bool compare_as_ints (double I, Double J)
{
Return (INT (I) <int (j ));
}
Int main (){
Double mydoubles [] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58 };
Vector <double> myvector;
Vector <double>: iterator it;
Myvector. Assign (mydoubles, mydoubles + 8 );
Cout <"using default comparison :";
Stable_sort (myvector. Begin (), myvector. End ());
For (IT = myvector. Begin (); it! = Myvector. End (); ++ it)
Cout <"" <* it;
Myvector. Assign (mydoubles, mydoubles + 8 );
Cout <"\ nusing 'compare _ as_ints ':";
Stable_sort (myvector. Begin (), myvector. End (), compare_as_ints );
For (IT = myvector. Begin (); it! = Myvector. End (); ++ it)
Cout <"" <* it;
Cout <Endl;
Return 0;
}
III,Partial_sort_copy: Sorts (partially sorts) elements of a range in a sequence)
Prototype:
Template <class inputiterator, class randomaccessiterator>
Randomaccessiterator
Partial_sort_copy (inputiterator first, inputiterator last,
Randomaccessiterator result_first,
Randomaccessiterator result_last );
Template <class inputiterator, class randomaccessiterator, class compare>
Randomaccessiterator
Partial_sort_copy (inputiterator first, inputiterator last,
Randomaccessiterator result_first,
Randomaccessiterator result_last, compare comp );
Sample Code:
// partial_sort example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myfunction (int i,int j) { return (i<j); }
int main () {
int myints[] = {9,8,7,6,5,4,3,2,1};
vector<int> myvector (myints, myints+9);
vector<int>::iterator it;
// using default comparison (operator <):
partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());
// using function as comp
partial_sort (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);
// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
4. partial_sort_copy: Partially sorted and copied
Prototype:
Template <class inputiterator, class randomaccessiterator>
Randomaccessiterator
Partial_sort_copy (inputiterator first, inputiterator last,
Randomaccessiterator result_first,
Randomaccessiterator result_last );
Template <class inputiterator, class randomaccessiterator, class compare>
Randomaccessiterator
Partial_sort_copy (inputiterator first, inputiterator last,
Randomaccessiterator result_first,
Randomaccessiterator result_last, compare comp );
Example:
// Partial_sort_copy example# Include <iostream># Include <algorithm># Include <vector>Using NamespaceSTD;BoolMyfunction (IntI,IntJ ){Return(I <j );}IntMain (){IntMyints [] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; vector <Int> Myvector (5); vector <Int>:: Iterator it;// Using default comparison (operator <):Partial_sort_copy (myints, myints + 9, myvector. Begin (), myvector. End ());// Using function as compPartial_sort_copy (myints, myints + 9, myvector. Begin (), myvector. End (), myfunction );// Print out content:Cout <"Myvector contains :";For(It = myvector. Begin (); it! = Myvector. End (); ++ it) cout <""<* It; cout <Endl;Return0;} 5,Nth_element: Place the nth element in the nth position of the sequence, and the element is smaller than the element before it. The element is later than the element, however, the first and second sequences are not necessarily ordered (sorted );
Prototype:Template <class randomaccessiterator>
Void nth_element (randomaccessiterator first, randomaccessiterator nth,
Randomaccessiterator last );
Template <class randomaccessiterator, class compare>
Void nth_element (randomaccessiterator first, randomaccessiterator nth,
Randomaccessiterator last, compare comp );
Sample Code:
// nth_element example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool myfunction (int i,int j) { return (i<j); }int main () { vector<int> myvector; vector<int>::iterator it; // set some values: for (int i=1; i<10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 random_shuffle (myvector.begin(), myvector.end()); // using default comparison (operator <): nth_element (myvector.begin(), myvector.begin()+5, myvector.end()); // using function as comp nth_element (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction); // print out content: cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; return 0;}