In the c ++ qsort () using the sort function
qsort function application Encyclopedia
seven kinds qsort sorted
<sort used herein are sorted in ascending>
a, sort the array of type int
int NUM [100];
the Sample:
int CMP ( const void * A, const void * B)
{
return * (int *) A - B * (int *);
}
qsort (NUM, 100, the sizeof (NUM [0]), CMP);
Second, char type arrays sorting (same type int)
char Word [100];
the Sample:
int CMP (const void * A, const void * B)
{
return * (char *) A - * (char *) B;
}
qsort (Word, 100, sizeof (word [0]), cmp);
three, ordered array of double type (with particular attention)
double in [100];
int CMP (A const void *, const void * B)
{
* return (Double *) A> * (Double *). 1 B:? -1;
}
qsort (in, 100, the sizeof (in [0]), CMP);
four, a sort of structure
struct the In
{
Double data;
int OTHER;
} S [100]
// to the general structure of the small value of the data is sorted, ordering on the type of key data structures in the body may be a variety of data, a write reference to the above examples
int cmp (const void * a, * B void const)
{
return (* (* the In) A) -> Data> (* (* the In) B) -> Data. 1:? -1;
}
qsort (S, 100, the sizeof (S [0]) , CMP);
five, two sort of structure
struct the In
{
int x;
int y;
} S [100];
// in ascending order according to x, when x is equal to y according to the descending order
int cmp ( A void * const, const void * B)
{
* C = the In struct (the In *) A;
struct the In * D = (the In *) B;
IF (the C-> X = D-> X!) the C-return> X - D-> X;
the else return D- > Y - the C-> Y;
}
qsort (S, 100, the sizeof (S [0]), CMP);
six, sort strings
struct the In
{
int Data;
char STR [100];
} S [100] ;
// Sort structural body lexicographically string str
int CMP (a const void *, const void * B)
{
return strcmp ((* (* the in) a) -> str, (* (* the in) B ) -> STR);
}
qsort (S, 100, the sizeof (S [0]), CMP);
seven, calculating the convex hull geometry seeking CMP
int CMP (a const void *, const void * B) // key cmp function, in addition to all the points of one point outside the rotation angle sort
{
struct point C * = (point *) a;
struct Point * D = (Point *) B;
IF (Calc (* C, * D, P [. 1]) <0) return. 1;
! the else IF (Calc (* C, * D, P [. 1]) && dis (c-> x, c-> y, p [1] .x, p [1] .y) <dis (d-> x, d-> y, p [1] .x, p [1] .y)) // if in a straight line, put away in front of the
return. 1;
the else return -1;
}
:
C ++ header file loaded "the iostream"
C qsort function are contained in <stdlib.h> head file, strcmp included in the <string.h> header file
In the c ++ qsort () using the sort function
qsort function application Encyclopedia
seven kinds qsort sorted
<sort used herein are sorted in ascending>
a, sort the array of type int
int NUM [100];
the Sample:
int CMP ( const void * A, const void * B)
{
return * (int *) A - B * (int *);
}
qsort (NUM, 100, the sizeof (NUM [0]), CMP);
Second, char type arrays sorting (same type int)
char Word [100];
the Sample:
int CMP (const void * A, const void * B)
{
return * (char *) A - * (char *) B;
}
qsort (Word, 100, sizeof (word [0]), cmp);
three, ordered array of double type (with particular attention)
double in [100];
int CMP (A const void *, const void * B)
{
* return (Double *) A> * (Double *). 1 B:? -1;
}
qsort (in, 100, the sizeof (in [0]), CMP);
four, a sort of structure
struct the In
{
Double data;
int OTHER;
} S [100]
// to the general structure of the small value of the data is sorted, ordering on the type of key data structures in the body may be a variety of data, a write reference to the above examples
int cmp (const void * a, * B void const)
{
return (* (* the In) A) -> Data> (* (* the In) B) -> Data. 1:? -1;
}
qsort (S, 100, the sizeof (S [0]) , CMP);
five, two sort of structure
struct the In
{
int x;
int y;
} S [100];
// in ascending order according to x, when x is equal to y according to the descending order
int cmp ( A void * const, const void * B)
{
struct the In * C = (the In *) A;
* D = the In struct (the In *) B;
IF (the C-> X = D-> X!) the C-return> X - D-> X;
the else return D-> Y - the C-> Y;
}
qsort ( s, 100, sizeof (s [ 0]), cmp);
six, sort strings
struct the in
{
int Data;
char str [100];
} S [100];
// structural body according to the string str lexicographically ordering
int CMP (const void * A, const void * B)
{
return strcmp ((* (the In *) A) -> STR, (* (the In *) B) -> STR);
}
qsort (S, 100, sizeof (s [0] ), cmp);
seven, calculating the convex hull geometry seeking cmp
int cmp (const void * a, * B const void) // cmp function key, in addition to all the points outside 1:00 rotation angle sort
{
struct Point C * = (Point *) A;
struct Point D * = (Point *) B;
IF (Calc (C *, D *, P [. 1]) <0) return. 1;
else if (! calc (* c , * d, p [1]) && dis (c-> x, c-> y, p [1] .x, p [1] .y) <dis (d-> x, d-> y, p [ 1] .x, p [1] .y)) // if in a straight line, put away in front of the
return. 1;
the else return -1;
}
:
C ++ loaded header file "the iostream"
C qsort function are contained in <stdlib.h> header file, strcmp contained in the <string.h> header file