hash_table.cpp

来自「采用迭代器模式实现hash算法」· C++ 代码 · 共 46 行

CPP
46
字号
#include "hash_table.h"



#ifdef UNITTEST
CPPUNIT_TEST_SUITE_REGISTRATION(HashTableTest);

void HashTableTest::setUp()
{
	printf("\n begin to test HashTable class\n");
}
void HashTableTest::tearDown()
{
	printf("\n end of testing HashTable class\n");
}
void HashTableTest::test_hashtable()
{
	typedef HashTable<int, int> Hash;
	Hash hash;
	Hash::Iterator it;
	//插入
	for (int i = 0; i < 100; i++)
		hash.insert (i, i*10);
	//遍历
	for (i = 0, it = hash.begin (); it != hash.end (); ++it, ++i) {
		CPPUNIT_ASSERT(it->key_ == i);
		CPPUNIT_ASSERT(it->val_ == i*10);
	}
	//查找
	it = hash.find (50);
	CPPUNIT_ASSERT(it != hash.end());
	CPPUNIT_ASSERT(it->key_ == 50);
	CPPUNIT_ASSERT(it->val_ == 50*10);
	//删除
	hash.erase (it);
	CPPUNIT_ASSERT(hash.size() == 99);
	it = hash.find(50);
	CPPUNIT_ASSERT(it == hash.end());
	printf("count:%d, buckcount:%d, buckelems:%d",
		hash.size(), hash.bucket_count(), hash.bucket_elems(30));
	//赋值
	Hash tmp, tmp2 = hash;
	tmp = hash;
}
#endif

⌨️ 快捷键说明

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