zoukankan      html  css  js  c++  java
  • hashtable的运用实例

     1 #include <hash_set>
     2 #include <iostream>
     3 using namespace std;
     4 int main()
     5 {
     6     hashtable<int,
     7         int,
     8         hash<int>,
     9         identify<int>,
    10         equal_to<int>,
    11         alloc> iht(50,hash<int>,equal_to<int>());    //指定保留50个bucket(桶)
    12     cout<<iht.size()<<endl;                //0
    13     cout<<iht.bucket_count()<<endl;        //53这是STL提供的最小质数
    14     cout<<iht.max_bucket_count()<<endl;    //4294967291
    15 
    16     iht.insert_unique(59);
    17     iht.insert_unique(63);
    18     iht.insert_unique(108);
    19     iht.insert_unique(2);
    20     iht.insert_unique(53);
    21     iht.insert_unique(55);
    22     cout<<iht.size()<<endl;                //6,此即hashtable<T>::num_elements
    23 
    24     /*一下声明一个hashtable迭代器*/
    25     hashtable<int,
    26         int,
    27         hash<int>,
    28         identify<int>,
    29         equal_to<int>,
    30         alloc>::iterator ite = iht.begin();
    31 
    32     for(int i = 0;i<iht.size();i++,ite++)
    33         cout<< *ite<<<' ';    //输出:53 55 2 108 59 63   并没有顺序
    34     cout<<endl;
    35 
    36     /*遍历所有buckets,如果其节点个数不为0,就打印节点个数,为0不打印*/
    37     for(int i = 0;i<iht.bucket_count();i++)
    38     {
    39         int n = iht.elem_in_bucket(i);
    40         if(n!=0)
    41             cout<<"bucket["<<i<<"] has"<<n<<" elems."<<endl;
    42     }
    43     /*
    44     输出:
    45     bucket[0] has 1 elems.
    46     bucket[2] has 3 elems.
    47     bucket[6] has 1 elems.
    48     bucket[10] has 1 elems.
    49     */
    50 
    51     /*为了验证“bucket(list)”的容量就是buckets vector的大小(这是从
    52     hashtable<T>::resize()得知的结果),我刻意将元素加到54个,
    53     看看是否发生重建表*/
    54 
    55     for(int i = 0;i<47;i++)
    56         iht.insert_equal(i);
    57     cout<<iht.size()<<endl;        //54
    58     cout<<iht.buck_count()<<endl;    //97确实扩容了(重建)
    59 
    60     for (inti; i<iht.bucket_count();i++ )
    61     {
    62         int n = iht.elems_in_bucket(i); 
    63         if(n!=0)
    64             cout<<"bucket["<<i<<"] has"<<n<<" elems."<<endl;
    65     }
    66     /*打印的结果
    67     bucket[2]到bucket[11]的节点个数为2
    68     其余的bucket[0]~bucket[47]的节点个数为1
    69     此外,bucket[53],[55],[59],[63]的节点个数均为1*/
    70 
    71     /*以迭代器变量hashtable,将所有节点打印出来*/
    72     ite = iht.begin();
    73     for(int i = 0;i<iht.size();i++,ite++)
    74         cout<< *ite<<<' ';
    75     cout<<endl;
    76 
    77     cout<<*(iht.find(2))<<endl;//2
    78     cout<<iht.count(2)<<endl;//2
    79     return 0;
    80 }
  • 相关阅读:
    Nhibernate 3.0 cookbook学习笔记 配置与架构
    jQuery 三级联动选项栏
    依赖注入框架Autofac学习笔记
    Windows服务初探
    再记面试题
    Nhibernate 3.0 cookbook学习笔记 一对多与多对多映射
    Nhibernate 3.0 cookbook学习笔记 创建一个加密类
    2011 微软 MVP 全球大会即将拉开序幕
    Windows Shell扩展系列文章 2 .NET 4为扩展的Windows Shell上下文菜单项添加位图图标
    【转】微软一站式示例代码库(中文版)20110413版本, 新添加16个Sample
  • 原文地址:https://www.cnblogs.com/houjun/p/4923255.html
Copyright © 2011-2022 走看看