hashset in c++
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
int main()
{
// empty map container
map<int, int> gquiz1;
// insert elements in random order
gquiz1.insert(pair<int, int>(1, 40));
gquiz1.insert(pair<int, int>(2, 30));
gquiz1.insert(pair<int, int>(3, 60));
gquiz1.insert(pair<int, int>(4, 20));
gquiz1.insert(pair<int, int>(5, 50));
gquiz1.insert(pair<int, int>(6, 50));
gquiz1.insert(pair<int, int>(7, 10));
// printing map gquiz1
map<int, int>::iterator itr;
cout << "\nThe map gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// assigning the elements from gquiz1 to gquiz2
map<int, int> gquiz2(gquiz1.begin(), gquiz1.end());
// print all elements of the map gquiz2
cout << "\nThe map gquiz2 after"
<< " assign from gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// remove all elements up to
// element with key=3 in gquiz2
cout << "\ngquiz2 after removal of"
" elements less than key=3 : \n";
cout << "\tKEY\tELEMENT\n";
gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
// remove all elements with key = 4
int num;
num = gquiz2.erase(4);
cout << "\ngquiz2.erase(4) : ";
cout << num << " removed \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// lower bound and upper bound for map gquiz1 key = 5
cout << "gquiz1.lower_bound(5) : "
<< "\tKEY = ";
cout << gquiz1.lower_bound(5)->first << '\t';
cout << "\tELEMENT = "
<< gquiz1.lower_bound(5)->second << endl;
cout << "gquiz1.upper_bound(5) : "
<< "\tKEY = ";
cout << gquiz1.upper_bound(5)->first << '\t';
cout << "\tELEMENT = "
<< gquiz1.upper_bound(5)->second << endl;
return 0;
}
#include <bitset>
// functional header
// for hash<class template> class
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// To demonstrate String Hashing
void stringHashing()
{
// Get the string
// to get its hash value
string hashing1 = "Geeks";
// Instatiation of Object
hash<string> mystdhash;
// Using operator() to get hash value
cout << "String hash values: "
<< mystdhash(hashing1)
<< endl;
}
// To demonstrate BITSET Hashing
void bitsetHashing()
{
// Get the BITSET
// to get its hash value
bitset<5> h_bitset("10101");
// Instatiation of Object
hash<bitset<5> > hash_bitset;
// Using operator() to get hash value
cout << "\nBitset 10101 hash value: "
<< hash_bitset(h_bitset) << endl;
}
// To demonstrate Vector<bool> Hashing
void vectorHashing()
{
// Get the Vector<bool>
// to get its hash value
vector<bool>
h_vecbool{ true, false,
true, false };
// Instatiation of Object
hash<vector<bool> > hash_vector_bool;
// Using operator() to get hash value
cout << "\nVector<bool> hash value: "
<< hash_vector_bool(h_vecbool)
<< endl;
}
// To demonstrate Char Hashing
void charHashing()
{
// Get the char
// to get its hash value
char ch = 'a';
// Instatiation of Object
hash<char> hash_char;
// Using operator() to get hash value
cout << "\nChar hash values: "
<< hash_char(ch)
<< endl;
}
// Driver Code
int main()
{
stringHashing();
bitsetHashing();
vectorHashing();
charHashing();
}