名前空間
変種
操作

「c/algorithm/bsearch」の版間の差分

提供: cppreference.com
< c‎ | algorithm
(r2.7.3) (ロボットによる 追加: de, en, es, fr, it, pt, ru, zh)
(Use {{lc}}. Update links. Various fixes.)
2行: 2行:
 
{{c/title|bsearch}}
 
{{c/title|bsearch}}
 
{{c/algorithm/navbar}}
 
{{c/algorithm/navbar}}
{{ddcl list begin}}
+
{{begin}}
{{ddcl list header | stdlib.h}}
+
{{header | stdlib.h}}
{{ddcl list item |
+
{{|
 
void* bsearch( const void* key, const void* ptr, size_t count, size_t size,
 
void* bsearch( const void* key, const void* ptr, size_t count, size_t size,
 
               int (*comp)(const void*, const void*) );
 
               int (*comp)(const void*, const void*) );
 
}}
 
}}
{{ddcl list end}}
+
{{end}}
  
 
{{tr|{{tt|key}}が指す配列内{{tt|ptr}}が指す要素に等しい要素を検索します。配列はsize{{tt|count}}の{{tt|size}}要素が含まれています。関数は{{tt|comp}}がオブジェクトの比較に使用される、によって指さ.|Finds an element equal to element pointed to by {{tt|key}} in an array pointed to by {{tt|ptr}}. The array contains {{tt|count}} elements of size {{tt|size}}. Function pointed to by {{tt|comp}} is used for object comparison.}}
 
{{tr|{{tt|key}}が指す配列内{{tt|ptr}}が指す要素に等しい要素を検索します。配列はsize{{tt|count}}の{{tt|size}}要素が含まれています。関数は{{tt|comp}}がオブジェクトの比較に使用される、によって指さ.|Finds an element equal to element pointed to by {{tt|key}} in an array pointed to by {{tt|ptr}}. The array contains {{tt|count}} elements of size {{tt|size}}. Function pointed to by {{tt|comp}} is used for object comparison.}}
  
 
===パラメータ===
 
===パラメータ===
{{param list begin}}
+
{{begin}}
{{param list item | key |{{tr| を検索するための要素へのポインタ| pointer to the element to search for}}}}
+
{{| key |{{tr| を検索するための要素へのポインタ| pointer to the element to search for}}}}
{{param list item | ptr |{{tr| 検討する配列へのポインタ| pointer to the array to examine}}}}
+
{{| ptr |{{tr| 検討する配列へのポインタ| pointer to the array to examine}}}}
{{param list item | count |{{tr| 配列内の要素の数| number of element in the array}}}}
+
{{| count |{{tr| 配列内の要素の数| number of element in the array}}}}
{{param list item | size |{{tr| バイト単位で配列内の各要素の大きさ| size of each element in the array in bytes}}}}
+
{{| size |{{tr| バイト単位で配列内の各要素の大きさ| size of each element in the array in bytes}}}}
{{param list ccmp | comp | {{tt|key}} is passed as the first argument, an element from the array as the second.}}
+
{{ccmp | comp | {{tt|key}} is passed as the first argument, an element from the array as the second.}}
{{param list end}}
+
{{end}}
  
 
===値を返します===
 
===値を返します===
{{tr|見つかった要素または別の方法で{{c|NULL}}へのポインタ..|pointer to the found element or {{c|NULL}} otherwise.}}
+
{{tr|見つかった要素または別の方法で{{|NULL}}へのポインタ..|pointer to the found element or {{|NULL}} otherwise.}}
  
 
===例===
 
===例===
60行: 60行:
  
 
===も参照してください===
 
===も参照してください===
{{dcl list begin}}
+
{{begin}}
{{dcl list template | c/algorithm/dcl list qsort}}
+
{{| c/algorithm/qsort}}
{{dcl list see cpp | cpp/algorithm/bsearch}}
+
{{see cpp | cpp/algorithm/bsearch}}
{{dcl list end}}
+
{{end}}
  
 
[[de:c/algorithm/bsearch]]
 
[[de:c/algorithm/bsearch]]

2013年7月2日 (火) 10:04時点における版

ヘッダ <stdlib.h> で定義
void* bsearch( const void* key, const void* ptr, size_t count, size_t size,
               int (*comp)(const void*, const void*) );
keyが指す配列内ptrが指す要素に等しい要素を検索します。配列はsizecountsize要素が含まれています。関数はcompがオブジェクトの比較に使用される、によって指さ.
Original:
Finds an element equal to element pointed to by key in an array pointed to by ptr. The array contains count elements of size size. Function pointed to by comp is used for object comparison.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

パラメータ

key -
を検索するための要素へのポインタ
Original:
pointer to the element to search for
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ptr -
検討する配列へのポインタ
Original:
pointer to the array to examine
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
count -
配列内の要素の数
Original:
number of element in the array
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
size -
バイト単位で配列内の各要素の大きさ
Original:
size of each element in the array in bytes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
comp - 第1引数が第2引数より小さい場合は負の整数値、第1引数が第2引数より大きい場合は正の整数値、第1引数と第2引数が同等な場合はゼロを返す比較関数。 key is passed as the first argument, an element from the array as the second.

比較関数のシグネチャは以下と同等であるべきです。

 int cmp(const void *a, const void *b);

関数は渡されたオブジェクトを変更してはならず、同じオブジェクトに対してはその配列内の位置にかかわらず一貫した結果を返さなければなりません。

値を返します

見つかった要素または別の方法でNULLへのポインタ..
Original:
pointer to the found element or NULL otherwise.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <stdlib.h>
#include <stdio.h>
 
struct data {
  int nr;
  char const *value;
} dat[] = {
  {1, "Foo"}, {2, "Bar"}, {3, "Hello"}, {4, "World"}
};
 
int data_cmp(void const *lhs, void const *rhs) {
  struct data const *const l = lhs;
  struct data const *const r = rhs;
  return l->nr < r->nr;
}
 
int main(void) {
  struct data key = { .nr = 3 };
  struct data const *res = bsearch(&key, dat, sizeof(dat)/sizeof(dat[0]),
                                   sizeof(dat[0]), data_cmp);
  if(!res) {
    printf("No %d not found\n", key.nr);
  }
  else {
    printf("No %d: %s\n", res->nr, res->value);
  }
}

出力:

No 3: Hello

も参照してください

不特定の型の要素の範囲をソートします
(関数) [edit]