Monday, October 3, 2011

Sort STL vector of C++

example:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>

using namespace std;

void showMyVector(const vector<int>& v);

int main()
{
vector<int> myVector;

cout << "Start with nothing" << endl;
showMyVector(myVector);

cout << "Insert something" << endl;
for (int i = 0; i < 10; i++) {
myVector.push_back(rand() % 100);
}
showMyVector(myVector);
cout << endl;

cout << "Sort the vector" << endl;
for (int i = 0; i < 10; i++) {
sort(myVector.begin(), myVector.end());
}
showMyVector(myVector);
cout << endl;

return 0;
}

void showMyVector(const vector<int>& v){
cout << "myVector.empty: " << v.empty() << endl;
cout << "myVector.size(): " << v.size() << endl;
for(unsigned i = 0; i < v.size(); i++){
cout << v[i] << " ";
}
cout << endl;
}


Sort STL vector of C++


No comments:

Post a Comment