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


C++ HashTable::hashFunct方法代码示例

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


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

示例1: main

int main()
{

	HashTable<int> hashtable;

	int inc = 0;
	int dec = 0;
	int ninehun = 0;
	int onehun = 0;
	for (int i = 0; i < MAXHASH; i++) {
		int hashed = hashtable.hashFunct(i);
		if (hashed > 500) {
			inc++;
		}
		if (hashed <= 500) {
			dec++;
		}
		if (hashed > 900) {
			ninehun++;
		}
		if (hashed < 100) {
			onehun++;
		}
		cout << i << "----------" << hashed << endl;
	}

	cout << "Keys above 500: " << inc << " | Keys less than 500: " << dec << " | Keys above 900: " << ninehun << " | Keys below 100: " << onehun << endl;
	int collisions = 0;
	int size = 0;
	for (size_t i = 0; i < MAXHASH; ++i) {
		collisions = 0;
		bool isGud = hashtable.insert(i, MAXHASH - i, collisions);
		if (isGud) {
			size++;
		}
		cout << "Inserted? " << isGud << endl;
		cout << "Number of collisions: " << collisions << endl;
		cout << "Actual size: " << size << endl;
	}

	collisions = 0;
	hashtable.insert(777, 11025101160059, collisions);
	int newVal = 0;
	bool isFound = hashtable.find(777, newVal);
	cout << "777 is found? " << isFound << endl;
	bool isGone = hashtable.remove(777);
	cout << "777 is gone? " << isGone << endl;
	isFound = hashtable.find(777, newVal);
	cout << "777 is found? (Should return false): " << isFound << endl;

	cout << "Actual size: " << size << endl;

	//Create test function to add random keys & keep track of number of collisions
	for (size_t n = 100; n <= 1000; n = n + 100) {
		for (size_t i = 0; i < MAXHASH; ++i) {
			collisions = 0;
			bool isGud = hashtable.remove(i);
			if (isGud) {
				--size;
			}
		}
		srand(MAXHASH);
		int random = rand() % 1000;
		vector<int> coll;
		for (size_t i = 0; i < n; ++i) {
			collisions = 0;
			bool isGud = hashtable.insert(i, random, collisions);
			if (isGud) {
				size++;
			}
			coll.push_back(collisions);
			//cout << "Inserted record with key " << i << "? " << isGud << endl;
			//cout << "Number of collisions: " << collisions << endl;
		}
		cout << "Attempted to insert " << n << " items." << endl;
		cout << "Actually inserted " << size << " items." << endl;

		int avg = 0;
		int denom = coll.size();
		for (size_t i = 0; i < MAXHASH; i++) {
			avg = avg + coll.at(i);
			coll.push_back(i);
		}
		avg = avg / denom;

		cout << "Average number of collisions for " << n << " inserts: " << avg << endl;
		cout << "The alpha value is: " << hashtable.alpha() << endl;
	}

	system("pause");
	return 0;
}
开发者ID:HannahMuetzel,项目名称:HashTable-Proj4,代码行数:92,代码来源:main.cpp


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