⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsearch.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>bsearch</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="bsearch"></a> <a name="tag_03_41"></a><!-- bsearch --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright &copy; 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_41_01"></a>NAME</h4><blockquote>bsearch - binary search a sorted table</blockquote><h4><a name="tag_03_41_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include &lt;<a href="../basedefs/stdlib.h.html">stdlib.h</a>&gt;<br><br> void *bsearch(const void *</tt><i>key</i><tt>, const void *</tt><i>base</i><tt>, size_t</tt> <i>nel</i><tt>,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size_t</tt> <i>width</i><tt>, int (*</tt><i>compar</i><tt>)(const void *, const void *));<br></tt></code></p></blockquote><h4><a name="tag_03_41_03"></a>DESCRIPTION</h4><blockquote><div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any conflict between therequirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers tothe ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div><p>The <i>bsearch</i>() function shall search 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>. If the <i>nel</i> argument has the value zero, the comparison function pointed to by <i>compar</i> shall not becalled and no match shall be found.</p><p>The comparison function pointed to by <i>compar</i> shall be called with two arguments that point to the <i>key</i> object andto an array element, in that order.</p><p>The application shall ensure that the comparison function pointed to by <i>compar</i> does not alter the contents of the array.The implementation may reorder elements of the array between calls to the comparison function, but shall not alter the contents ofany individual element.</p><p>The implementation shall ensure that the first argument is always a pointer to the key.</p><p>When the same objects (consisting of width bytes, irrespective of their current positions in the array) are passed more thanonce to the comparison function, the results shall be consistent with one another. That is, the same object shall always comparethe same way with the key.</p><p>The application shall ensure that the function returns an integer less than, equal to, or greater than 0 if the <i>key</i>object is considered, respectively, to be less than, to match, or to be greater than the array element. The application shallensure that the array consists of all the elements that compare less than, all the elements that compare equal to, and all theelements that compare greater than the <i>key</i> object, in that order.</p></blockquote><h4><a name="tag_03_41_04"></a>RETURN VALUE</h4><blockquote><p>The <i>bsearch</i>() function shall return a pointer to a matching member of the array, or a null pointer if no match is found.If two or more members compare equal, which member is returned is unspecified.</p></blockquote><h4><a name="tag_03_41_05"></a>ERRORS</h4><blockquote><p>No errors are defined.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_41_06"></a>EXAMPLES</h4><blockquote><p>The example below searches a table containing pointers to nodes consisting of a string and its length. The table is orderedalphabetically on the string in the node pointed to by each entry.</p><p>The code fragment below reads in strings and either finds the corresponding node and prints out the string and its length, orprints an error message.</p><pre><tt>#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;#include &lt;string.h&gt;<br>#define TABSIZE    1000<br>struct 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 *)(&amp;node),               (void *)table, TABSIZE,               sizeof(struct node), node_compare);        if (node_ptr != NULL) {            (void)printf("string = %20s, length = %d\n",                node_ptr-&gt;string, node_ptr-&gt;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)-&gt;string,        ((const struct node *)node2)-&gt;string);}</tt></pre></blockquote><h4><a name="tag_03_41_07"></a>APPLICATION USAGE</h4><blockquote><p>The pointers to the key and the element at the base of the table should be of type pointer-to-element.</p><p>The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to thevalues being compared.</p><p>In practice, the array is usually sorted according to the comparison function.</p></blockquote><h4><a name="tag_03_41_08"></a>RATIONALE</h4><blockquote><p>The requirement that the second argument (hereafter referred to as <i>p</i>) to the comparison function is a pointer to anelement of the array implies that for every call all of the following expressions are non-zero:</p><pre><tt>((char *)p - (char *(base) % width == 0(char *)p &gt;= (char *)base(char *)p &lt; (char *)base + nel * width</tt></pre></blockquote><h4><a name="tag_03_41_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_41_10"></a>SEE ALSO</h4><blockquote><p><a href="hcreate.html"><i>hcreate</i>()</a> , <a href="lsearch.html"><i>lsearch</i>()</a> , <a href="qsort.html"><i>qsort</i>()</a> , <a href="tsearch.html"><i>tsearch</i>()</a> , the Base Definitions volume ofIEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/stdlib.h.html"><i>&lt;stdlib.h&gt;</i></a></p></blockquote><h4><a name="tag_03_41_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_41_12"></a>Issue 6</h4><blockquote><p>The DESCRIPTION is updated to avoid use of the term &quot;must&quot; for application requirements.</p><p>IEEE&nbsp;Std 1003.1-2001/Cor&nbsp;1-2002, item XSH/TC1/D6/11 is applied, adding to the DESCRIPTION the last sentence of thefirst non-shaded paragraph, and the following three paragraphs. The RATIONALE section is also updated. These changes are foralignment with the ISO&nbsp;C standard.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -