qsort_s
執行快速排序。版本的qsort中所述的安全性增強功能與安全性功能,則在 CRT 中。
void qsort_s(
void *base,
size_t num,
size_t width,
int (__cdecl *compare )(void *, const void *, const void *),
void * context
);
參數
base
目標陣列的開頭。num
陣列中元素的大小。width
項目大小,以位元組為單位。compare
比較函式。第一個引數是context指標。第二個引數是變數的指標, key搜尋。第三個引數是陣列元素,以相較於指標key。context
變數的指標,內容可以是任何物件的compare常式需要存取。
備註
qsort_s函式實作快速排序演算法排序陣列的num項目,每個width個位元組。引數base是指向基底陣列會依序排列。qsort_s使用已排序的項目將會覆寫此陣列。引數compare為使用者提供的常式,會比較兩個陣列項目,並傳回值,指定兩者的關聯性的指標。qsort_s呼叫compare例行的一或多個時間期間排序,每次呼叫時,將指標傳遞至兩個陣列項目:
compare( context, (void *) & elem1, (void *) & elem2 );
常式必須比較項目,則會傳回下列值之一:
傳回值 |
描述 |
---|---|
< 0 |
elem1小於elem2 |
0 |
elem1對等用法elem2 |
> 0 |
elem1大於elem2 |
陣列會依序遞增的順序,所定義的比較函式。若要排序陣列遞減順序排序,請反轉,因為 「 大於 」 與 「 小於 」 中的比較函式。
如果不正確的參數傳遞給函式,不正確的參數處理常式會叫用,如所述參數驗證。若要繼續,請允許執行,則函式會傳回與errno設定為 [ EINVAL。如需詳細資訊,請參閱 errno、 _doserrno、 _sys_errlist 和 _sys_nerr。
錯誤狀況
索引鍵 |
base |
compare |
num |
width |
errno |
---|---|---|---|---|---|
NULL |
任何 |
任何 |
任何 |
任何 |
EINVAL |
任何 |
NULL |
任何 |
!= 0 |
任何 |
EINVAL |
任何 |
任何 |
任何 |
任何 |
< = 0 |
EINVAL |
任何 |
任何 |
NULL |
任何 |
任何 |
EINVAL |
qsort_s具有相同的行為qsort ,但context參數和集合errno。藉由傳遞context參數,比較函式可用的物件指標來存取物件的功能問題或無法存取其他資訊透過項目指標。額外的context參數會讓qsort_s更安全,因為context可用來避免使用靜態變數時,讓您可以使用共享的資訊所引入的重新進入錯誤compare函式。
需求
常式 |
所需的標頭 |
---|---|
qsort_s |
<stdlib.h> 和 <search.h> |
其他的相容性資訊,請參閱相容性在簡介中。
定義函數庫: 所有版本的CRT 程式庫功能。
範例
下列範例會示範如何使用context中的參數qsort_s函式。context參數會更容易執行安全執行緒的排序。而不是使用此捷徑來確保執行緒安全的靜態變數時,傳遞不同的context在每個主要的參數。在本例中的地區設定物件用作為context參數。
// crt_qsort_s.cpp
// compile with: /EHsc /MT
#include <stdlib.h>
#include <stdio.h>
#include <search.h>
#include <process.h>
#include <locale.h>
#include <locale>
#include <windows.h>
using namespace std;
// The sort order is dependent on the code page. Use 'chcp' at the
// command line to change the codepage. When executing this application,
// the command prompt codepage must match the codepage used here:
#define CODEPAGE_850
#ifdef CODEPAGE_850
// Codepage 850 is the OEM codepage used by the command line,
// so \x00e1 is the German Sharp S in that codepage and \x00a4
// is the n tilde.
char *array1[] = { "wei\x00e1", "weis", "annehmen", "weizen", "Zeit",
"weit" };
char *array2[] = { "Espa\x00a4ol", "Espa\x00a4" "a", "espantado" };
char *array3[] = { "table", "tableux", "tablet" };
#define GERMAN_LOCALE "German_Germany.850"
#define SPANISH_LOCALE "Spanish_Spain.850"
#define ENGLISH_LOCALE "English_US.850"
#endif
#ifdef CODEPAGE_1252
// If using codepage 1252 (ISO 8859-1, Latin-1), use \x00df
// for the German Sharp S and \x001f for the n tilde.
char *array1[] = { "wei\x00df", "weis", "annehmen", "weizen", "Zeit",
"weit" };
char *array2[] = { "Espa\x00f1ol", "Espa\x00f1" "a", "espantado" };
char *array3[] = { "table", "tableux", "tablet" };
#define GERMAN_LOCALE "German_Germany.1252"
#define SPANISH_LOCALE "Spanish_Spain.1252"
#define ENGLISH_LOCALE "English_US.1252"
#endif
// The context parameter lets you create a more generic compare.
// Without this parameter, you would have stored the locale in a
// static variable, thus making sort_array vulnerable to thread
// conflicts.
int compare( void *pvlocale, const void *str1, const void *str2)
{
char s1[256];
char s2[256];
strcpy_s(s1, 256, *(char**)str1);
strcpy_s(s2, 256, *(char**)str2);
_strlwr_s( s1, sizeof(s1) );
_strlwr_s( s2, sizeof(s2) );
locale& loc = *( reinterpret_cast< locale * > ( pvlocale));
return use_facet< collate<char> >(loc).compare(s1,
&s1[strlen(s1)], s2, &s2[strlen(s2)]);
}
void sort_array(char *array[], int num, locale &loc)
{
qsort_s(array, num, sizeof(char*), compare, &loc);
}
void print_array(char *a[], int c)
{
for (int i = 0; i < c; i++)
printf("%s ", a[i]);
printf("\n");
}
void sort_german(void * Dummy)
{
sort_array(array1, 6, locale(GERMAN_LOCALE));
}
void sort_spanish(void * Dummy)
{
sort_array(array2, 3, locale(SPANISH_LOCALE));
}
void sort_english(void * Dummy)
{
sort_array(array3, 3, locale(ENGLISH_LOCALE));
}
int main( )
{
int i;
HANDLE threads[3];
printf("Unsorted input:\n");
print_array(array1, 6);
print_array(array2, 3);
print_array(array3, 3);
// Create several threads that perform sorts in different
// languages at the same time.
threads[0] = reinterpret_cast<HANDLE>(
_beginthread( sort_german , 0, NULL));
threads[1] = reinterpret_cast<HANDLE>(
_beginthread( sort_spanish, 0, NULL));
threads[2] = reinterpret_cast<HANDLE>(
_beginthread( sort_english, 0, NULL));
for (i = 0; i < 3; i++)
{
if (threads[i] == reinterpret_cast<HANDLE>(-1))
{
printf("Error creating threads.\n");
exit(1);
}
}
// Wait until all threads have terminated.
WaitForMultipleObjects(3, threads, true, INFINITE);
printf("Sorted output: \n");
print_array(array1, 6);
print_array(array2, 3);
print_array(array3, 3);
}
範例輸出
Unsorted input:
weiß weis annehmen weizen Zeit weit
Español España espantado
table tableux tablet
Sorted output:
annehmen weiß weis weit weizen Zeit
España Español espantado
table tablet tableux