Demonstrating sort() in vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void init_v(vector<int> &vectorObject)
{
vectorObject[0] = 1;
vectorObject[1] = 10;
vectorObject[2] = 2;
vectorObject[3] = -3;
vectorObject[4] = 9;
vectorObject[5] = 4;
vectorObject[6] = 8;
vectorObject[7] = 5;
vectorObject[8] = 7;
vectorObject[9] = 6;
}
int main()
{
vector<int> vectorObject(10);
int i;
// sort the entire container
init_v(vectorObject);
cout << "Sort an entire container.";
cout << "Original order:";
for(i = 0; i <10; i++)
cout << vectorObject[ i ] << " ";
cout << endl;
sort(vectorObject.begin(), vectorObject.end());
cout << "Order after sorting container:";
for(i = 0; i <10; i++)
cout << vectorObject[ i ] << " ";
cout << endl;
// sort a subset of the container
init_v(vectorObject);
cout << "Sort a subset of a container.";
cout << "Original order:";
for(i = 0; i <10; i++)
cout << vectorObject[ i ] << " ";
cout << endl;
sort(vectorObject.begin() + 2, vectorObject.end() - 2);
cout << "Order after sorting vectorObject[2] through vectorObject[7]:";
for(i = 0; i <10; i++)
cout << vectorObject[ i ] << " ";
cout << endl;
return 0;
}
Related examples in the same category