qsort-库函数
#include <iostream> #include <cstdlib> using namespace std; int compare_ints( const void* a, const void* b ) { int* arg1 = (int*) a; int* arg2 = (int*) b; if( *arg1 < *arg2 ) return -1; else if( *arg1 == *arg2 ) return 0; else return 1; } int main() { int n = 5; int x[] = {5,1,7,4,2}; qsort(x,n,sizeof(int),compare_ints); for ( int i=0; i<=n-1; i++ ) cout << x[i] << " "; cout << endl; return 0; }