C
Christian Christmann
Hi,
I've a class htable.cpp with a private member: map<string, void*>
stringhash;
and a function:
"void HashTable::Insert(char* ky,void* entr) {
strhash.insert(pair<string, void*>(ky, entr));
}"
When adding some values in the driver program with: "
HashTable testIntHT;
char* eins = "eins";
char* zwei = "zwei";
char* drei = "drei";
char* vier = "vier";
testIntHT.Insert(1, eins);
testIntHT.Insert(2, zwei);
testIntHT.Insert(3, drei);
testIntHT.Insert(4, vier);
"
and printing the values with the function: "void
HashTable:
rintStringHT()
{
std::map<string, void*>::const_iterator iter; cout << endl;
for (iter = strhash.begin(); iter != strhash.end(); ++iter) {
cout << iter->first << 't' << iter->second << 'n';
}
}"
I get an output:
drei 0x3
eins 0x1
vier 0x4
zwei 0x2
-----------------
Now, I want to replace this class by a template class in order to add
arbitrary values to the hashtable. The new template class htableT.cpp:
"template <class KEYTYPE> class HashTableT {
std::map<KEYTYPE, void*> hash;
[snip] "
with the insert function:
"template <class KEYTYPE>
void HashTableT<KEYTYPE>::Insert(KEYTYPE ky,void* entr) {
hash.insert(pair<KEYTYPE, void*>(ky, entr));
}
"
After inserting the values with:
"
HashTableT<int> testIntHT;
char* eins = "eins";
char* zwei = "zwei";
char* drei = "drei";
char* vier = "vier";
testIntHT.Insert(1, eins);
testIntHT.Insert(2, zwei);
testIntHT.Insert(3, drei);
testIntHT.Insert(4, vier);
"
and printing the values with the function: template <class KEYTYPE>
void HashTableT<KEYTYPE>:
rintHashTable() {
typename std::map<KEYTYPE, void*>::const_iterator iter = hash.begin();
cout << endl;
for (iter = hash.begin(); iter != hash.end(); ++iter) {
cout << iter->first << 't' << iter->second << 'n';
}
}
I get:
eins 0x1
zwei 0x2
drei 0x3
vier 0x4
As you can see there is a difference in the outputs. The first map is
sorting the values alphabetically while the second is keeping the order
the values have been inserted.
How can I get the same behaviour with my template insert function ie. what
do I have to do to have the STL map sort the values automatically after
they have been inserted? Thanks
Chris
I've a class htable.cpp with a private member: map<string, void*>
stringhash;
and a function:
"void HashTable::Insert(char* ky,void* entr) {
strhash.insert(pair<string, void*>(ky, entr));
}"
When adding some values in the driver program with: "
HashTable testIntHT;
char* eins = "eins";
char* zwei = "zwei";
char* drei = "drei";
char* vier = "vier";
testIntHT.Insert(1, eins);
testIntHT.Insert(2, zwei);
testIntHT.Insert(3, drei);
testIntHT.Insert(4, vier);
"
and printing the values with the function: "void
HashTable:
{
std::map<string, void*>::const_iterator iter; cout << endl;
for (iter = strhash.begin(); iter != strhash.end(); ++iter) {
cout << iter->first << 't' << iter->second << 'n';
}
}"
I get an output:
drei 0x3
eins 0x1
vier 0x4
zwei 0x2
-----------------
Now, I want to replace this class by a template class in order to add
arbitrary values to the hashtable. The new template class htableT.cpp:
"template <class KEYTYPE> class HashTableT {
std::map<KEYTYPE, void*> hash;
[snip] "
with the insert function:
"template <class KEYTYPE>
void HashTableT<KEYTYPE>::Insert(KEYTYPE ky,void* entr) {
hash.insert(pair<KEYTYPE, void*>(ky, entr));
}
"
After inserting the values with:
"
HashTableT<int> testIntHT;
char* eins = "eins";
char* zwei = "zwei";
char* drei = "drei";
char* vier = "vier";
testIntHT.Insert(1, eins);
testIntHT.Insert(2, zwei);
testIntHT.Insert(3, drei);
testIntHT.Insert(4, vier);
"
and printing the values with the function: template <class KEYTYPE>
void HashTableT<KEYTYPE>:
typename std::map<KEYTYPE, void*>::const_iterator iter = hash.begin();
cout << endl;
for (iter = hash.begin(); iter != hash.end(); ++iter) {
cout << iter->first << 't' << iter->second << 'n';
}
}
I get:
eins 0x1
zwei 0x2
drei 0x3
vier 0x4
As you can see there is a difference in the outputs. The first map is
sorting the values alphabetically while the second is keeping the order
the values have been inserted.
How can I get the same behaviour with my template insert function ie. what
do I have to do to have the STL map sort the values automatically after
they have been inserted? Thanks
Chris