Or
#include <algorithm>
It has several sorts IIRC
Yes, in essence:
Code:
#include <iostream>
#include <algorithm>
using namespace std;
struct myStruct {
int key;
double data;
bool operator<(const myStruct& r) const { return key < r.key; }
};
template <typename T> int arrayElements(const T& r) {
return sizeof r / sizeof *r;
}
int main() {
int i;
myStruct myStructArray[] = {
{ 5, 5.1 },
{ 1, 6.2 },
{ 6, 2.2 },
{ -1, 50.0 }
};
for(i = 0; i < arrayElements(myStructArray); ++i) {
cout << myStructArray[i].key << ' ' << myStructArray[i].data << endl;
}
sort(myStructArray, myStructArray + arrayElements(myStructArray));
cout << endl;
for(i = 0; i < arrayElements(myStructArray); ++i) {
cout << myStructArray[i].key << ' ' << myStructArray[i].data << endl;
}
}
Which is more C++ than qsort, in my humble opinion. It's also more flexible, since sort works on any object that behaves like an array.