📄 hashprobe.cpp~
字号:
#include <time.h>
#include <algae/config.h>
#include <iostream>
#include <cstdlib>
#include <strstream>
#include <string>
#include <algae/algae.h>
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)
{
algae->promptForInput (prompt, 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;
set->show();
}
}
void hashInsert()
{
init();
string v;
algae->promptForInput("String to insert", v);
bool inserted = set->insert (v);
if (!inserted)
cout << "Unable to insert " << v << " into table." << endl;
Visible::unHighlightAll();
}
void hashSearch()
{
init();
string v;
algae->promptForInput("String to search for", v);
int k = set->count (v);
cout << "count() returned " << k << endl;
Visible::unHighlightAll();
}
void hashErase()
{
init();
string v;
algae->promptForInput("String to remove", v);
set->erase (v);
Visible::unHighlightAll();
}
void hashClear()
{
init();
set->clear ();
Visible::unHighlightAll();
}
AlgAE* algae;
int main (int nargs, char** args)
{
time_t dummy;
srand (time(&dummy));
algae = new AlgAE(nargs, args);
algae->menuItem("Insert", hashInsert);
algae->menuItem("Erase", hashErase);
algae->menuItem("Search (count)", hashSearch);
algae->menuItem("-", 0);
algae->menuItem("Clear", hashClear);
algae->run ("Hashing - linear probing",
"Hash table with linear probing adapted for AlgAE animation by\n"
" Steven J. Zeil, Old Dominion University"
);
delete algae;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -