当前位置: 首页>>代码示例>>C++>>正文


C++ HashMap::loadFactor方法代码示例

本文整理汇总了C++中HashMap::loadFactor方法的典型用法代码示例。如果您正苦于以下问题:C++ HashMap::loadFactor方法的具体用法?C++ HashMap::loadFactor怎么用?C++ HashMap::loadFactor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HashMap的用法示例。


在下文中一共展示了HashMap::loadFactor方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: allocator

static std::string testInit()
{
    PoolAllocator allocator(sizeof(HashMap<int,std::string>::Entry));
    StandardInitializer<std::string> initializer;
    StandardHash<int> hash;
    HashMap<int,std::string>* map;

    map = new HashMap<int,std::string>();
    if( !map->empty() or map->size() != 0 ) {
        return "Size not initialized to 0 (a).";
    }
    delete map;

    map = new HashMap<int,std::string>(100);
    if( !map->empty() or map->size() != 0 ) {
        return "Size not initialized to 0 (b).";
    }
    delete map;

    map = new HashMap<int,std::string>(100, allocator);
    if( !map->empty() or map->size() != 0 ) {
        return "Size not initialized to 0 (c).";
    }
    delete map;

    map = new HashMap<int,std::string>(100, allocator, initializer);
    if( !map->empty() or map->size() != 0 ) {
        return "Size not initialized to 0 (d).";
    }
    delete map;

    map = new HashMap<int,std::string>(100, allocator, initializer, hash);
    if( !map->empty() or map->size() != 0 ) {
        return "Size not initialized to 0 (d).";
    }
    delete map;

    map = new HashMap<int,std::string>(10, allocator, initializer, hash, 0.5);
    if( !map->empty() or map->size() != 0 ) {
        return "Size not initialized to 0 (e).";
    } else if( map->loadFactor() != 0.5 ) {
        //printf("%f\n", map->loadFactor());
        std::cout << map->loadFactor() << std::endl;
        return "LoadFactor not properly initialized.";
    }
    delete map;

    return "";
}
开发者ID:staticimport,项目名称:promotelib,代码行数:49,代码来源:HashMapTest.cpp

示例2:

TEST(TestHashMapBucketCount, emptyShouldHaveSize0)
{
    HashMap Empty;
    ASSERT_EQ(0, Empty.bucketCount());
}

TEST(TestHashMapLoadFactor, 5Items10BucketsHalfLoad)
{
    HashMap Test;
    Test.add("Ford", "Tang");
    Test.add("Diana", "Chang");
    Test.add("Bronx", "Cat");
    Test.add("Alora", "Cat");
    Test.add("Bailey", "Cat");
    ASSERT_EQ(0.5, Test.loadFactor());
}

TEST(TestHashMapMaxBucketSize, emptyShouldHaveMax0)
{
    HashMap Empty;
    ASSERT_EQ(0, Empty.maxBucketSize());
}

TEST(TestHashMapClearBucket, clearedHashMapSizeis0)
{
    HashMap Test;
    Test.add("Ford", "Tang");
    Test.clearBucket();
    ASSERT_EQ(0, Test.size());
}
开发者ID:FordTang,项目名称:ICS45C_ProgrammingCplusplus,代码行数:30,代码来源:HashMapTests.cpp

示例3: main

int main(int argc, char **argv)
{
  size_t nkeys = (1 << 16);
  size_t nlookup = 100000;
  double lf = 0.75;
  
  if (argc >= 2)
  {
    int n;
    if (sscanf(argv[1], "%d", &n) == 1)
    {
      std::cout << "Use " << n << " keys" << std::endl;
      nkeys = size_t(n);
    }
  }
  if (argc >= 3)
  {
    int l;
    if (sscanf(argv[2], "%d", &l) == 1)
    {
      std::cout << "Do " << l << " lookups" << std::endl;
      nlookup = size_t(l);
    }
  }
  if (argc >= 4)
  {
    double d;
    if (sscanf(argv[3], "%lf", &d) == 1)
    {
      std::cout << "Use max load factor: " << d << std::endl;
      lf = d;
    }
  }
  
  size_t ndups = 0;
  std::cout << "Testing insertion and search with " << nkeys << " keys" << std::endl;
  
  HashMap<std::string, std::string> hmap;
  
  hmap.setMaxLoadFactor(lf);
  
  std::vector<std::string> allKeys;
  
  allKeys.resize(nkeys);
  
  for (size_t i=0; i<nkeys; ++i)
  {
    std::string s = RandomString();
    allKeys[i] = s;
    if (!hmap.insert(s, s))
    {
      std::cout << "Duplicate key found: \""  << s << "\"" << std::endl;
      ++ndups;
    }
  }
  
  std::cout << hmap.size() << " entries (" << ndups << " keys were duplicate)" << std::endl;
  std::cout << "Load Factor of " << hmap.loadFactor() << std::endl;
  
  for (size_t i=0; i<nkeys; ++i)
  {
    size_t ik = rand() % nkeys;
    const std::string &k = allKeys[ik];
    const std::string &v = hmap[k];
    if (v != k)
    {
      throw std::runtime_error("key/value mistmatch");
    }
  }
  
  HashMap<std::string, std::string>::KeyVector keys;
  HashMap<std::string, std::string>::ValueVector vals;
  
  hmap.keys(keys);
  hmap.values(vals);
  
  assert(hmap.size() == keys.size());
  assert(hmap.size() == vals.size());
  
  for (size_t i=0; i<hmap.size(); ++i)
  {
    if (keys[i] != vals[i])
    {
      throw std::runtime_error("key/value pair mismatch");
    }
  }
  
  {
    HashMap<std::string, std::string> hmap;
    std::map<std::string, std::string> smap;
    size_t numhits = 0;
    
    hmap.setMaxLoadFactor(lf);
    
    std::vector<std::string> allKeys, extraKeys;
    std::string val;
    
    allKeys.resize(nkeys);
    extraKeys.resize(nkeys);
    
//.........这里部分代码省略.........
开发者ID:gatgui,项目名称:gcore,代码行数:101,代码来源:test_hashmap.cpp


注:本文中的HashMap::loadFactor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。