📄 bsearch.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>bsearch</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_001_314"> </a>NAME</h4><blockquote>bsearch - binary search a sorted table</blockquote><h4><a name = "tag_000_001_315"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="stdlib.h.html">stdlib.h</a>>void *bsearch(const void *<i>key</i>, const void *<i>base</i>, size_t <i>nel</i>, size_t <i>width</i>, int (*<i>compar</i>)(const void *, const void *));</code></pre></blockquote><h4><a name = "tag_000_001_316"> </a>DESCRIPTION</h4><blockquote>The<i>bsearch()</i>function searches an array of<i>nel</i>objects, the initial element of which is pointed to by<i>base</i>,for an element that matches the object pointed to by<i>key</i>.The size of each element in the array is specified by<i>width</i>.<p>The comparison function pointed to by<i>compar</i>is called with two arguments that point to the<i>key</i>object and to an array element, in that order.<p>The function must return an integer less than, equal to, orgreater than 0 if the<i>key</i>object is considered, respectively, to be less than, to match, orto be greater than the array element.The array must consist of: all the elements that compare lessthan, all the elements that compare equal to, and all theelements that compare greater than the<i>key</i>object, in that order.</blockquote><h4><a name = "tag_000_001_317"> </a>RETURN VALUE</h4><blockquote>The<i>bsearch()</i>function returns a pointer to a matching member of the array, ora null pointer if no match is found.If two or more members compare equal, which member is returned isunspecified.</blockquote><h4><a name = "tag_000_001_318"> </a>ERRORS</h4><blockquote>No errors are defined.</blockquote><h4><a name = "tag_000_001_319"> </a>EXAMPLES</h4><blockquote>The example below searches a table containing pointersto nodes consisting of a string and its length.The table is ordered alphabetically on the string in thenode pointed to by each entry.<p>The code fragment below reads in strings and either finds thecorresponding node and prints out the string and its length,or prints an error message.<pre><code>#include <stdio.h>#include <stdlib.h>#include <string.h>#define TABSIZE 1000struct node { /* these are stored in the table */ char *string; int length;};struct node table[TABSIZE]; /* table to be searched */ . . .{ struct node *node_ptr, node; /* routine to compare 2 nodes */ int node_compare(const void *, const void *); char str_space[20]; /* space to read string into */ . . . node.string = str_space; while (scanf("%s", node.string) != EOF) { node_ptr = (struct node *)bsearch((void *)(&node), (void *)table, TABSIZE, sizeof(struct node), node_compare); if (node_ptr != NULL) { (void)printf("string = %20s, length = %d\n", node_ptr->string, node_ptr->length); } else { (void)printf("not found: %s\n", node.string); } }}/* This routine compares two nodes based on an alphabetical ordering of the string field.*/intnode_compare(const void *node1, const void *node2){ return strcoll(((const struct node *)node1)->string, ((const struct node *)node2)->string);}</code></pre></blockquote><h4><a name = "tag_000_001_320"> </a>APPLICATION USAGE</h4><blockquote>The pointers to the key and the element at the base ofthe table should be of type pointer-to-element.<p>The comparison function need not compare every byte,so arbitrary data may be contained in the elements in addition to the valuesbeing compared.<p>In practice, the array is usually sorted according to thecomparison function.<br></blockquote><h4><a name = "tag_000_001_321"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_001_322"> </a>SEE ALSO</h4><blockquote><i><a href="hsearch.html">hsearch()</a></i>,<i><a href="lsearch.html">lsearch()</a></i>,<i><a href="qsort.html">qsort()</a></i>,<i><a href="tsearch.html">tsearch()</a></i>,<i><a href="stdlib.h.html"><stdlib.h></a></i>.</blockquote><h4>DERIVATION</h4><blockquote>Derived from Issue 1 of the SVID.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -