📄 hcreate.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>hcreate</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_005_899"> </a>NAME</h4><blockquote>hcreate, hdestroy, hsearch - manage hash search table</blockquote><h4><a name = "tag_000_005_900"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="search.h.html">search.h</a>>int hcreate(size_t <i>nel</i>);void hdestroy(void);ENTRY *hsearch (ENTRY <i>item</i>, ACTION <i>action</i>);</code></pre></blockquote><h4><a name = "tag_000_005_901"> </a>DESCRIPTION</h4><blockquote>The<i>hcreate()</i>,<i>hdestroy()</i>and<i>hsearch()</i>functions manage hash search tables.<p>The<i>hcreate()</i>function allocates sufficient space for the table, and must be called before<i>hsearch()</i>is used. The<i>nel</i>argument is an estimate of the maximum number of entries thatthe table will contain.This number may be adjusted upward by thealgorithm in order to obtain certain mathematically favourablecircumstances.<p>The<i>hdestroy()</i>function disposes of the search table, and may be followed by another call to<i>hcreate()</i>.After the call to<i>hdestroy()</i>,the data can no longer be considered accessible.<p>The<i>hsearch()</i>function is a hash-table search routine. It returns a pointer into a hashtable indicating the location at which an entry can be found. The<i>item</i>argument is a structure of type<b>ENTRY</b>(defined in the<i><a href="search.h.html"><search.h></a></i>header) containing two pointers:<i>item.key</i>points to the comparison key (a<b>char *</b>),and<i>item.data</i>(a<b>void *</b>)points to any other data to be associated with that key.The comparison function used by<i>hsearch()</i>is<i><a href="strcmp.html">strcmp()</a></i>.The<i>action</i>argument is a member of an enumeration type<b>ACTION</b>indicating the disposition of the entry if it cannot befound in the table.ENTER indicates that the item should be inserted in the table at anappropriate point.FIND indicates that no entry should be made.Unsuccessful resolution is indicated by the return of a null pointer.</blockquote><h4><a name = "tag_000_005_902"> </a>RETURN VALUE</h4><blockquote>The<i>hcreate()</i>function returns 0 if it cannot allocate sufficient space for thetable, and returns non-zero otherwise.<p>The<i>hdestroy()</i>function returns no value.<p>The<i>hsearch()</i>function returns a null pointer if either the action is FINDand the item could not be found or the action isENTER and the table is full.</blockquote><h4><a name = "tag_000_005_903"> </a>ERRORS</h4><blockquote>The<i>hcreate()</i>and<i>hsearch()</i>functions may fail if:<dl compact><dt>[ENOMEM]<dd>Insufficient storage space is available.</dl></blockquote><h4><a name = "tag_000_005_904"> </a>EXAMPLES</h4><blockquote>The following example will read in strings followed by twonumbers and store them in a hash table, discarding duplicates.It will then read in strings and find the matching entryin the hash table and print it out.<pre><code>#include <stdio.h>#include <search.h>#include <string.h>struct info { /* this is the info stored in the table */ int age, room; /* other than the key. */};#define NUM_EMPL 5000 /* # of elements in search table */int main(void){ char string_space[NUM_EMPL*20]; /* space to store strings */ struct info info_space[NUM_EMPL]; /* space to store employee info*/ char *str_ptr = string_space; /* next space in string_space */ struct info *info_ptr = info_space;/* next space in info_space */ ENTRY item; ENTRY *found_item; /* name to look for in table */ char name_to_find[30]; int i = 0; /* create table; no error checking is performed */ (void) hcreate(NUM_EMPL); while (scanf("%s%d%d", str_ptr, &info_ptr->age, &info_ptr->room) != EOF && i++ < NUM_EMPL) { /* put information in structure, and structure in item */ item.key = str_ptr; item.data = info_ptr; str_ptr += strlen(str_ptr) + 1; info_ptr++; /* put item into table */ (void) hsearch(item, ENTER); } /* access table */ item.key = name_to_find; while (scanf("%s", item.key) != EOF) { if ((found_item = hsearch(item, FIND)) != NULL) { /* if item is in the table */ (void)printf("found %s, age = %d, room = %d\n", found_item->key, ((struct info *)found_item->data)->age, ((struct info *)found_item->data)->room); } else (void)printf("no such employee %s\n", name_to_find); } return 0;}</code></pre></blockquote><h4><a name = "tag_000_005_905"> </a>APPLICATION USAGE</h4><blockquote>The<i>hcreate()</i>and<i>hsearch()</i>functions may use<i><a href="malloc.html">malloc()</a></i>to allocate space.</blockquote><h4><a name = "tag_000_005_906"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_005_907"> </a>SEE ALSO</h4><blockquote><i><a href="bsearch.html">bsearch()</a></i>,<i><a href="lsearch.html">lsearch()</a></i>,<i><a href="malloc.html">malloc()</a></i>,<i><a href="strcmp.html">strcmp()</a></i>,<i><a href="tsearch.html">tsearch()</a></i>,<i><a href="search.h.html"><search.h></a></i>.<br></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 + -