本文整理汇总了C++中HashTable::containsKey方法的典型用法代码示例。如果您正苦于以下问题:C++ HashTable::containsKey方法的具体用法?C++ HashTable::containsKey怎么用?C++ HashTable::containsKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashTable
的用法示例。
在下文中一共展示了HashTable::containsKey方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
TEST(HashTableTest, expandKey) {
HashTable<std::string,std::string> ht;
char buf[10];
std::string c;
for(int i = 0; i < 100; i ++)
{
sprintf(buf,"a%d",i);
//ht.add(buf, "ab");
ht[buf] = "ab";
ht[buf] = "ac";
c = ht[buf];
std::cout << "buf:" << buf << ",value:" << ht[buf] << ",c:" << c << std::endl;
}
//std::cout << "11:" << ht["11"]<< std::endl;
EXPECT_EQ(100UL,ht.count());
ht.remove("a8");
EXPECT_EQ(99UL,ht.count());
//EXPECT_TRUE(ht["a13"] == "ac");
EXPECT_TRUE(ht.containsKey("a13"));
EXPECT_TRUE(ht.contains("a13","ac"));
EXPECT_FALSE(ht.containsKey("a1012"));
EXPECT_FALSE(ht.contains("a1123","ac"));
EXPECT_FALSE(ht.contains("a12","ab"));
for(int i = 0; i < 1000; i ++)
{
sprintf(buf,"a%d",i);
//ht.add(buf, "ab");
ht[buf] = "ab";
ht[buf] = "ac";
c = ht[buf];
std::cout << "buf:" << buf << ",value:" << ht[buf] << ",c:" << c << std::endl;
}
ht.remove("a18");
EXPECT_EQ(999UL,ht.count());
//EXPECT_TRUE(ht["a13"] == "ac");
EXPECT_TRUE(ht.containsKey("a13"));
EXPECT_TRUE(ht.contains("a13","ac"));
EXPECT_TRUE(ht.containsKey("a112"));
EXPECT_FALSE(ht.contains("a1203","ac"));
}
示例2: checkPlay
//option 1, check a play to see if it is legal
void checkPlay(HashTable<string,int> dictionary) {
cout << "\nEnter the word you wish to play: ";
string word;
cin >> word; //getting the word from user
word = lowercase(word); //make word lowercase
if (dictionary.containsKey(word)) { //check if it is in the dictionary or not
cout << "Congratulations! You have found a legal play!\n" << endl;
} else {
cout << "Sorry, that is not a legal play.\n" << endl;
}
menu(dictionary); //back to menu
}
示例3: main
int main() {
cout << "\nWelcome to Scrabble Solver\n" << endl;
HashTable<string,int> dictionary; //allocate dictionary
ifstream inFile("/usr/local/doc/sowpods.txt");
string word;
int counter = 0;
while (inFile >> word) { //load the dictionary
word = lowercase(word);
dictionary.insert(word, counter);
++counter;
assert(dictionary.containsKey(word));
}
menu(dictionary);
return 0;
}
示例4: main
int main(){
HashTable<string,string> ht;
string key="Key#",num,aa,bb,val="Value#";
for(int i=0;i<16;i++){
num = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
aa = key + num;
bb = val + num;
ht.insert(aa,bb);
}
cout << ht.size()<<endl;
ht.printAll();
for(int i=0;i<16;i++){
num = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
aa = key + num;
cout << ht._delete(aa)<<endl;
}
val = "second#";
for(int i=20;i<30;i++){
num = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
aa = key + num;
bb = val + num;
ht.insert(aa,bb);
}
ht.printAll();
cout << ht.size()<<endl;
cout << (ht.containsKey("Key#22")?"Found\n":"NOT Found\n");
cout << (ht.containsValue("second#22")?"Found\n":"NOT Found\n");
ht.iterator();
Entry<string,string>* entry;
while(ht.hasNext()){
entry = ht.next();
cout<<"Key = "<< entry->getKey()<<" | Value = "<< entry->getValue()<<endl;
}
return 0;
}
示例5: main
int main()
{
// print my name and this assignment's title
cout << "LAB 10: Write, Test, and Apply The HashTable Class Template\n";
cout << "Programmer: Jacky Chow\n";
cout << "Editor(s) used: Notepad++\n";
cout << "Compiler(s) used: Visual C++\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
int n = 100;
int arraySize;
HashTable<string, TermSection, 1009> a(hashCode);
cout << "Created HashTable<string, TermSection, 1009> a(hashCode)\n";
cout << "a.capacity returns: " << a.capacity() << endl;
assert(807 == a.capacity());
cout << "Size should be 0. a.size returns: " << a.size() << endl << endl;
assert(0 == a.size());
char* token;
char buf[1000];
const char* const tab = "\t";
int counter = 0;
ifstream fin;
fin.open("dvc-schedule.txt");
if (!fin.good()) throw "I/O error";
while (fin.good())
{
//progress bar
if(counter == n) break;
// read the line
string line;
getline(fin, line);
strcpy(buf, line.c_str());
if (buf[0] == 0) continue;
// parse the line
const string term(token = strtok(buf, tab));
const string section(token = strtok(0, tab));
const string course((token = strtok(0, tab)) ? token : "");
const string instructor((token = strtok(0, tab)) ? token : "");
const string whenWhere((token = strtok(0, tab)) ? token : "");
if (course.find('-') == string::npos) continue; // invalid line
const string subjectCode(course.begin(), course.begin() + course.find('-'));
counter++;
TermSection x = {term, section};
a[x] = course;
}
fin.close();
vector<TermSection> k = a.keys();
cout << "Capacity of a should now be 807. a.capacity returns: " << a.capacity() << endl;
assert(807 == a.capacity());
cout << "Size should also be 99. a.size returns: " << a.size() << endl;
assert(99 == a.size());
cout << "keys vector size should be equal to a.size: " << k.size() << endl;
arraySize = k.size();
assert(arraySize == a.size());
counter = 0;
// object copy testing
{
const HashTable<string, TermSection, 1009> copy = a;
cout << endl;
k = copy.keys();
cout << "Capacity of copy1 should be 807. copy.capacity returns: " << copy.capacity() << endl;
assert(807 == copy.capacity());
cout << "Size of copy1 should also be 99. copy.size returns: " << copy.size() << endl;
assert(99 == copy.size());
cout << "keys vector size of copy1 should be equal to copy.size: " << k.size() << endl;
arraySize = k.size();
assert(arraySize == copy.size());
cout << "Printing out course from copy1 using element 4 in vector k: " << copy[k[4]] << endl;
cout << "Now testing that copy.containsKey() will return true with that parameter.\n";
if(copy.containsKey(k[4])) cout << "copy.containsKey(k[4]) returned True.\n";
else cout << "Error! a.containsKey(k[4]) returned False.\n";
assert(copy.containsKey(k[4]));
TermSection y = {"Horch 2036", "9999"};
cout << "Printing out course with fake y parameter(should be a dummy): " << copy[y] << endl;
cout << "Now testing that copy.containsKey() will return false with fake y parameter.\n";
if(!copy.containsKey(y)) cout << "copy.containsKey(y) returned False.\n";
else cout << "Error! copy.containsKey(y) returned True.\n";
assert(!copy.containsKey(y));
}
// object assignment testing
{
cout << endl;
HashTable<string, TermSection, 1009> copy(hashCode);
cout << "Created copy 2 with a capacity 807.\nIt returns " << copy.capacity() << endl;
assert(807 == copy.capacity());
cout << "Copy 2 should have a size of 0. It returns: " << copy.size() << endl;
cout << "Assigning copy 2 to equal a.\n";
copy = a;
k = copy.keys();
cout << "Capacity of copy2 should now be 160. copy.capacity returns: " << copy.capacity() << endl;
assert(807 == copy.capacity());
//.........这里部分代码省略.........