📄 hashprobe.cpp~
字号:
#include <time.h>
#include <iostream>
#include <cstdlib>
#include <strstream>
#include <string>
string itos (int i)
{
char buf[16];
ostrstream str (buf, 16);
str << i << ends;
return buf;
}
#include "hashset.h"
using namespace std;
void promptForInput(const char* prompt, int& input)
{
string reply;
bool OK = false;
while (!OK)
{
cout << prompt << flush;
cin >> reply;
const char* replystr = reply.c_str();
istrstream in (replystr);
OK = !!(in >> input);
}
}
struct StringHash
{
int operator() (const string& s) const {return s.length();}
};
typedef hash_set<string, 8, StringHash> HashTable;
HashTable* set;
bool firstTime = true;
void init()
{
if (firstTime)
{
firstTime = false;
set = new HashTable;
}
}
void hashInsert()
{
init();
string v;
cout << "String to insert? " << flush;
cin >> v;
bool inserted = set->insert (v);
if (!inserted)
cout << "Unable to insert " << v << " into table." << endl;
}
void hashSearch()
{
init();
string v;
cout << "String to search for? " << flush;
cin >> v;
int k = set->count (v);
cout << "count() returned " << k << endl;
}
void hashErase()
{
init();
string v;
cout << "String to remove? " << flush;
cin >> v;
set->erase (v);
}
void hashClear()
{
init();
set->clear ();
}
int main (int nargs, char** args)
{
time_t dummy;
srand (time(&dummy));
cout << "Hashing - linear probing\n" << endl;
int reply;
while (1)
{
cout << "1. Insert\n2. Erase\n3. Search\n4. Clear\n0. Quit"
<< endl;
promptForInput("Your choice? ", reply);
if (reply < 1 || reply > 4)
break;
switch (reply) {
case 1:
hashInsert();
break;
case 2:
hashErase();
break;
case 3:
hashSearch();
break;
case 4:
hashClear();
break;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -