本文整理汇总了C++中Hash::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ Hash::insert方法的具体用法?C++ Hash::insert怎么用?C++ Hash::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Hash
的用法示例。
在下文中一共展示了Hash::insert方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
Hash<int,string> h;
if(h.find(111)!=0)
cout<<*(h.find(111))<<endl;
else
cout<<111<<" Not exist"<<endl;
h.insert(111,"111");
h.insert(222,"222");
h.insert(333,"333");
if(h.find(111)!=0)
cout<<*(h.find(111))<<endl;
else
cout<<111<<"Not exist"<<endl;
if(h.find(222)!=0)
cout<<*(h.find(222))<<endl;
else
cout<<222<<"Not exist"<<endl;
h.remove(111);
cout<<"Remove 111 ..."<<endl;
if(h.find(111)!=0)
cout<<*(h.find(111))<<endl;
else
cout<<111<<" Not exist"<<endl;
}
示例2: main
int main(){
Hash<int>* h = new Hash<int>();
h->insert("one", 1);
std::cout << "inserted (\"one\", 1)\nresult: " << h->get("one") << std::endl;
h->insert("one", 2);
std::cout << "replaced 1 with 2\n" << h->get("one") << std::endl;
h->insert("noe", 3);
std::cout << "inserted (\"noe\", 3) (should create the same hash as \"one\", but with value of 3)\nresult: " << h->get("noe") << std::endl;
h->insert("ZZZZZZzZzZzZzzzZUHcuhu", 239);
h->insert("ZZZZZZzZzZZzzzzZUHcuhu", 13);
std::cout << "inserted (\"ZZZZZZzZzZzZzzzZUHcuhu\", 239)\n" << h->get("ZZZZZZzZzZzZzzzZUHcuhu") << std::endl;
h->remove("noe");
std::cout << "removed (\"noe\"\n" << std::endl;
h->print();
return 0;
}
示例3: main
int main(){
Hash<int>* tab = new Hash<int>(100);
tab->insert("chickin" , 5);
tab->insert("car" , 1);
tab->insert("asdf", 3);
cout << tab->find("chickin") << endl;
cout << tab->find("asdf") << endl;
tab->remove("car");
cout << tab->find("car");
delete tab;
return 0;
}
示例4: toHash
Attachment::Hash Attachment::toHash(const Attachment::List &list)
{
Hash hash;
foreach (auto attachment, list)
hash.insert(attachment.type(), attachment);
return hash;
}
示例5: main
int main(int argc, char *argv[])
{
string lineBuffer;
ifstream file;
file.open(argv[1]);
int count=0;
Hash strHash;
vector<string> wordArray;
//insert strings into vector
int g=0;
while (!file.eof()){
getline(file, lineBuffer);
string str=new char[lineBuffer.length()]();
if (lineBuffer.length() != 0)
wordArray.push_back(lineBuffer);
}
for(int i=0;i<wordArray.size();i++)
strHash.insert((char*)&wordArray.at(i)[0]);
//sort vector by string length
sort(wordArray.begin(),wordArray.end());
sort(wordArray.begin(),wordArray.end(), stringCompare);
for(int i=0;i<wordArray.size();i++){
//check whether this word is made up of other words
if(buildWord(wordArray.at(i), true, strHash)){
count++;
//only print first 2 longest word made up of other words
if(count<3)
cout<<wordArray.at(i)<<endl;
}
}
//printf how many of the words in the list can be constructed of other words in the list
cout<<"There are "<<count<<" words that can be constructed of other words in the list"<<endl;
return 0;
}
示例6: main
int main(void)
{
int val;
Hash<int, int>* h = new Hash<int, int>(11);
h->insert(80);
h->insert(40);
h->insert(65);
h->insert(24);
h->insert(58);
if (h->Search(80, val))
cout << "the val exist: " << val << endl;
else
cout << "the val does not exist!" << endl;
int a[] = {1,2,3,4,5,6,5,6,7,1,3,4,5,6,7,9,100,10};
int b[] = {1,2,3,4,5,6,7,8,9,10};
int c[] = {5,2,3,1,100,100};
int d[] = {1};
if (is_duplicate1(c, sizeof(c)/sizeof(int)))
cout << "exist duplicate!" << endl;
else
cout << "does not exist duplicate!" << endl;
return 0;
}
示例7: mode
IntPair mode(const int *values, unsigned int size) {
Hash::iterator it;
Hash hash;
for (int i = 0; i < size; i++) {
it = hash.find(values[i]);
if(it == hash.end()) {
hash.insert( std::make_pair(values[i], 1));
} else {
it->second += 1;
}
}
unsigned int counts = 0;
Hash::iterator found;
for (it = hash.begin(); it != hash.end(); ++it) {
if(it->second > counts) {
counts = it->second;
found = it;
}
}
return *found;
}
示例8: BuildHashTable
//------------------------------------------------------------------------------
void BuildHashTable(std::vector<Particle> &p_list, Hash &hash_table)
{
int num_particles = p_list.size();
int grid_x;
int grid_y;
int grid_z;
float cell_size = (g_xmax - g_ymin) / GRID_RESOLUTION;
hash_table.clear();
for (int i = 0; i < num_particles; i++)
{
grid_x = floor(p_list[i].position[0] / cell_size);
grid_y = floor(p_list[i].position[1] / cell_size);
grid_z = floor(p_list[i].position[2] / cell_size);
p_list[i].hash = ComputeHash(grid_x, grid_y, grid_z);
hash_table.insert(Hash::value_type(p_list[i].hash, i));
}
}
示例9: main
int main(int argc, char const *argv[])
{
int n, q, a, b;
char str[10], str2[10];
while(~scanf("%d%d", &n, &q))
{
memset(head, 0, (n+1)*sizeof(int));
memset(dist, 0, (n+1)*sizeof(int));
memset(visit, 0, (n+1)*sizeof(bool));
memset(qhead, 0, sizeof(qhead));
memset(qmap, 0, sizeof(qmap));
surnames.clear();
qhead_hash.clear();
qmap_hash.clear();
for (int i = 0; i < maxn; ++i) id_to_name[i].clear();
for (int i = 0; i < maxn; ++i) name_to_id[i].clear();
for (int i = 0; i < n; ++i)
{
scanf("%s", str);
id_to_name[i+1] = str;
name_to_id[surnames.insert(str)].push_back(i+1);
}
for (int i = 1; i <= n-1; ++i)
{
scanf("%d%d", &a, &b);
edge[i*2-1].to = b;
edge[i*2-1].next = head[a];
head[a] = i*2-1;
edge[i*2].to = a;
edge[i*2].next = head[b];
head[b] = i*2;
}
for (int i = 1; i <= q; ++i)
{
scanf("%s%s", str, str2);
if ((surnames.findHash(str) == -1) || (surnames.findHash(str2) == -1))
{
qq[i].a = qq[i].b = "";
continue;
}
qq[i].a = str;
qq[i].b = str2;
if (qhead_hash.findHash(str) == -1) qhead[qhead_hash.insert(str)] = 0;
if (qhead_hash.findHash(str2) == -1) qhead[qhead_hash.insert(str2)] = 0;
qedge[i*2-1].to = str2;
qedge[i*2-1].next = qhead[qhead_hash.findHash(str)];
qhead[qhead_hash.findHash(str)] = i*2-1;
qedge[i*2].to = str;
qedge[i*2].next = qhead[qhead_hash.findHash(str2)];
qhead[qhead_hash.findHash(str2)] = i*2;
qmap[qmap_hash.insert(make_pair(str, str2))] = -1;
qmap[qmap_hash.insert(make_pair(str2, str))] = -1;
}
LCA(1, 0);
for (int i = 1; i <= q; ++i)
{
if (qq[i].a == "" || qq[i].b == "")
{
printf("-1\n");
}
else
{
printf("%d\n", qmap[qmap_hash.findHash(make_pair(qq[i].a, qq[i].b))] + 1);
}
}
}
return 0;
}
示例10: QVERIFY
void tst_QHash::insert1()
{
const char *hello = "hello";
const char *world = "world";
const char *allo = "allo";
const char *monde = "monde";
{
typedef QHash<QString, QString> Hash;
Hash hash;
QString key;
for (int i = 0; i < 10; ++i) {
key[0] = i + '0';
for (int j = 0; j < 10; ++j) {
key[1] = j + '0';
hash.insert(key, "V" + key);
}
}
for (int i = 0; i < 10; ++i) {
key[0] = i + '0';
for (int j = 0; j < 10; ++j) {
key[1] = j + '0';
hash.remove(key);
}
}
}
{
typedef QHash<int, const char *> Hash;
Hash hash;
hash.insert(1, hello);
hash.insert(2, world);
QVERIFY(hash.size() == 2);
QVERIFY(!hash.isEmpty());
{
Hash hash2 = hash;
hash2 = hash;
hash = hash2;
hash2 = hash2;
hash = hash;
hash2.clear();
hash2 = hash2;
QVERIFY(hash2.size() == 0);
QVERIFY(hash2.isEmpty());
}
QVERIFY(hash.size() == 2);
{
Hash hash2 = hash;
hash2[1] = allo;
hash2[2] = monde;
QVERIFY(hash2[1] == allo);
QVERIFY(hash2[2] == monde);
QVERIFY(hash[1] == hello);
QVERIFY(hash[2] == world);
hash2[1] = hash[1];
hash2[2] = hash[2];
QVERIFY(hash2[1] == hello);
QVERIFY(hash2[2] == world);
hash[1] = hash[1];
QVERIFY(hash[1] == hello);
}
{
Hash hash2 = hash;
hash2.detach();
hash2.remove(1);
QVERIFY(hash2.size() == 1);
hash2.remove(1);
QVERIFY(hash2.size() == 1);
hash2.remove(0);
QVERIFY(hash2.size() == 1);
hash2.remove(2);
QVERIFY(hash2.size() == 0);
QVERIFY(hash.size() == 2);
}
hash.detach();
{
Hash::iterator it1 = hash.find(1);
QVERIFY(it1 != hash.end());
Hash::iterator it2 = hash.find(0);
QVERIFY(it2 != hash.begin());
QVERIFY(it2 == hash.end());
*it1 = monde;
QVERIFY(*it1 == monde);
QVERIFY(hash[1] == monde);
*it1 = hello;
QVERIFY(*it1 == hello);
QVERIFY(hash[1] == hello);
//.........这里部分代码省略.........
示例11: main
/* 测试主函数 */
int main()
{
Hash<int> h;
h.insert(4);
h.insert(34);
h.insert(54);
h.insert(35);
h.insert(8);
h.insert(22);
h.insert(48);
h.insert(9);
h.insert(32);
h.insert(2);
h.insert(3);
h.insert(54);
h.insert(37);
h.insert(8);
h.insert(54);
h.insert(6);
h.insert(9);
h.insert(12);
h.insert(12);
h.insert(23);
h.insert(37);
h.insert(48);
h.insert(54);
h.insert(66);
h.insert(9);
h.insert(12);
h.Print();
system("pause");
return 0;
}
示例12: main
int main()
{
Hash<char> toTest (hasher, 10);
// isEmpty() test
cout << "\nisEmpty() Test" << endl;
cout << "isEmpty(): " << toTest.isEmpty() << endl;
cout << "isFull(): " << toTest.isFull() << endl;
toTest.showStructure();
// insert() test
cout << "\ninsert() Test" << endl;
for (char c = '0'; c <= 'z'; c++)
{
toTest.insert(c);
}
toTest.showStructure();
// isFull() test
cout << "\nisFull() Test" << endl;
cout << "isEmpty(): " << toTest.isEmpty() << endl;
cout << "isFull(): " << toTest.isFull() << endl;
// remove() and retrieve() test
cout << "\nremove() and retrieve() Test" << endl;
char rtest = ' ';
cout << "remove(3): " << toTest.remove(3) << endl;
bool rreturn = toTest.retrieve(3, rtest);
cout << "retrieve(3, rtest): " << rreturn << " | rtest = " << rtest << endl;
toTest.showStructure();
// clear() test
cout << "\nclear() Test" << endl;
toTest.clear();
toTest.showStructure();
// isEmpty() test
cout << "\nisEmpty() Test" << endl;
cout << "isEmpty(): " << toTest.isEmpty() << endl;
cout << "isFull(): " << toTest.isFull() << endl;
toTest.showStructure();
// Int test version
Hash<int> testTwo(hasher_two, 20);
cout << "\n\nTesting with ints:" << endl;
// insert() test
cout << "\ninsert() Test" << endl;
for (int i = 1; i <= 115; i++)
{
testTwo.insert(i);
}
testTwo.showStructure();
// isFull() test
cout << "\nisFull() Test" << endl;
cout << "isEmpty(): " << testTwo.isEmpty() << endl;
cout << "isFull(): " << testTwo.isFull() << endl;
// remove() and retrieve() test
cout << "\nremove() and retrieve() Test" << endl;
cout << "remove(3): " << testTwo.remove(3) << endl;
int itest;
bool ireturn = testTwo.retrieve(3, itest);
cout << "retrieve(3, rtest): " << ireturn << " | rtest = " << itest << endl;
testTwo.showStructure();
// clear() test
cout << "\nclear() Test" << endl;
testTwo.clear();
testTwo.showStructure();
system("pause");
}
示例13: main
int main(int argc, char *argv[])
{
ofstream log; // to record log file
log.open("log.txt", std::fstream::app);
CommandLineParser clp(argv[1],','); //instantiating the class CommandLineParser
if (argc == 1)
{
log << "Error: no argument is passed.\n" << endl;
log.close();
return -1;
}
//use CommandLineParser to get the script name
char *script = clp.extract("script");
if (script == NULL || *script == '\0')
{
log << "Error: no script file specified.\n" << endl;
log.close();
return -1;
}
//use ScriptParser to get the commands from script file
ScriptParser SParser = ScriptParser();
ifstream indata(script);
if (!indata.is_open()) // check if script file is correctly opened.
{
log << "Error: Script file \'" << script << "\' cannot be opened or does not exist.\n" << endl;
log.close();
return -1;
}
//start parsing script file
int i = 0;
int n;
int size;
int k;
string line; // store each line in script file
WordList *L; // array of word lists
FreqList *F; // array of frequency lists which store the information of words and its frequency
string *S; // array of strings which store the name of lists
Hash HT; // hash table to store all the words
L = new WordList[1000];
F = new FreqList[1000];
S = new string[1000];
//initiate temporary lists and list nodes
WordNode *tempWordNode;
FreqNode *tempFreqNode;
WordList tempWordList;
WordList *tempWordListpt;
FreqList tempFreqList;
FreqList tempFreqList2;
string tempString;
Node *tempNode;
int pos1;
int pos2;
log << "Begin parsing script file \'" << script << "\':\n" << endl;
while(getline(indata, line))
{
log << line << '\n';
SParser.ParseLine(line);
//process to determine if listID exits
int ID = -1; // cmd.listID, after following checking process, if it's still -1, then cmd.listID is a new list, otherwise cmd.listID is the word list L[ID] or frequency list F[ID]
int ID2 = -1; // cmd.value1 (if it stores list name), same procesure as above
int ID3 = -1; // cmd.value2 (if it stores list name), same procesure as above
for (int j = 0; j < i; j++)
{
ID = (SParser.cmd.listID == S[j])?j:ID; // check if cmd.listID is already in S[]
}
if (SParser.operationCode() != 3 && SParser.operationCode() != 8 && SParser.operationCode() != 9 && SParser.operationCode() != 10 && SParser.operationCode() != 13 && SParser.operationCode() != 0 && ID == -1)
{
log << "Error: invalid operation, because list \'" << SParser.cmd.listID << "\' is not created yet.\n" << endl;
if (SParser.operationCode() == 4 || SParser.operationCode() == 5)
{
ofstream output;
output.open(SParser.cmd.value1.c_str());
output << "List " << SParser.cmd.listID << " does not exist" << endl;
output.close();
}
continue;
}
if ((SParser.operationCode() == 3 || SParser.operationCode() == 8 || SParser.operationCode() == 9 || SParser.operationCode() == 10 || SParser.operationCode() == 13) && ID > -1)
{
log << "Error: invalid operation, because list \'" << SParser.cmd.listID << "\' already exists.\n" << endl;
continue;
}
// do all the list operations, such as read, insert, delete, write, intersection, union, load, filter, index and seach
switch (SParser.operationCode())
{
case 1:
n = L[ID].insert(SParser.cmd.value2, SParser.cmd.value1);
if (n == -1) {
//.........这里部分代码省略.........