stl Sort in C++; simple code for sorting the array of structures
Simple Code that shows how to use the C++ STL function sort()
<blockquote>
#include <iostream>
#include <algorithm>
using namespace std;
struct Numb{
double num;
int pos1;
bool operator <(const Numb &n1){
return num < n1.num;
};
};
////////////////////////////////
void main(void){
Numb numb[10];
for(int i=0; i < 10; i++){
numb[i].num = rand();
numb[i].pos1 = i;
}
cout << endl << "---------" <<endl;
for(int i=0; i < 10; i++){
cout << " " << "(" << numb[i].pos1 << ") " << numb[i].num;
}
cout << endl << "---------" <<endl;
std::sort( numb, numb+10);
cout << endl << "---------" <<endl;
for(int i=0; i < 10; i++){
cout << " " << "(" << numb[i].pos1 << ") " << numb[i].num;
}
cout << endl << "---------" <<endl;
}