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::printStringHT()
{
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>::printHashTable() {
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 3 1582
On Thu, 14 Apr 2005 17:36:55 +0200, Christian Christmann wrote: 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::printStringHT() { 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>::printHashTable() { 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
Sorry, I posted the wrong code:
Here is the correct one but the problem remains:
The working insert function:
void LargeHashTable::Insert(char* ky,void* entr)
{
strhash.insert(pair<string, void*>(ky, entr));
}
And the inserted values:
"
HashTable testCharHT;
testCharHT.Insert("eins", (void*)1);
testCharHT.Insert("zwei", (void*)2);
testCharHT.Insert("drei", (void*)3);
testCharHT.Insert("vier", (void*)4);
"
The output:
drei 0x3
eins 0x1
vier 0x4
zwei 0x2
The not working new function:
template <class KEYTYPE>
void HashTableT<KEYTYPE>::Insert(KEYTYPE ky,void* entr)
{
hash.insert(pair<KEYTYPE, void*>(ky, entr));
}
The the inserted values:
"
HashTableT<char*> testCharHT;
testCharHT.Insert("eins", (void*)1);
testCharHT.Insert("zwei", (void*)2);
testCharHT.Insert("drei", (void*)3);
testCharHT.Insert("vier", (void*)4);
"
The new output:
eins 0x1
zwei 0x2
drei 0x3
vier 0x4
Chris
Christian Christmann wrote: 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";
BAD IDEA(tm). Do not initialise char* with a literal. testIntHT.Insert(1, eins); testIntHT.Insert(2, zwei); testIntHT.Insert(3, drei); testIntHT.Insert(4, vier); "
That shouldn't work. Your 'Insert' function expects a char*, and you
are passing a number. Are you sure you're talking about working code? and printing the values with the function: "void HashTable::printStringHT() { 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
REALLY? I somehow doubt that. What compiler allowed you to convert
integer numbers into char*? -----------------
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";
You should definitely avoid initialising pointers to non-const char with
string literals. While it's allowed for compatibility with C, it is
a BAD IDEA(tm). 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>::printHashTable() { 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
REALLY? Do you mean to say that when you print out 'iter->first', which
should be an int, you get strings "eins", etc., and when you print out
't' you get some whitespace? Whom are you trying to fool here? Post
real code.
As you can see there is a difference in the outputs.
Difference? As in "it doesn't correspond to the code"?
The first map is sorting the values alphabetically while the second is keeping the order the values have been inserted.
If you need to keep the insertion order, use std::list instead of
std::map.
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
It _does_ sort them automatically. It sorts them based on the 'key' you
provide. And you provided *integers* from 1 to 4 as keys. You do get
your values sorted. The order just *happens* to be the same as insertion
order.
All screw-up aside, you need to make the key of the second (the template)
variation a 'string', not an integer.
V
Christian Christmann wrote: On Thu, 14 Apr 2005 17:36:55 +0200, Christian Christmann wrote:
Hi,
I've a class htable.cpp with a private member: map<string, void*> stringhash;
stringhash or strhash? Why don't you actually post _real_ code? and a function: "void HashTable::Insert(char* ky,void* entr) { strhash.insert(pair<string, void*>(ky, entr)); }" [...] Sorry, I posted the wrong code:
No joke?
Here is the correct one but the problem remains:
The working insert function:
void LargeHashTable::Insert(char* ky,void* entr) { strhash.insert(pair<string, void*>(ky, entr)); }
And the inserted values: " HashTable testCharHT; testCharHT.Insert("eins", (void*)1); testCharHT.Insert("zwei", (void*)2); testCharHT.Insert("drei", (void*)3); testCharHT.Insert("vier", (void*)4); "
The output: drei 0x3 eins 0x1 vier 0x4 zwei 0x2
The not working new function: template <class KEYTYPE> void HashTableT<KEYTYPE>::Insert(KEYTYPE ky,void* entr) { hash.insert(pair<KEYTYPE, void*>(ky, entr)); }
The the inserted values: " HashTableT<char*> testCharHT; testCharHT.Insert("eins", (void*)1); testCharHT.Insert("zwei", (void*)2); testCharHT.Insert("drei", (void*)3); testCharHT.Insert("vier", (void*)4); "
The new output: eins 0x1 zwei 0x2 drei 0x3 vier 0x4
While keys that are 'string' have 'std::less' comparator that actually
compares the _contents_ of the strings, if you define your map to have
the Key a 'char*', the comparison of those does NOT go into the _contents_
of the memory, it only compares the _values_ of the pointers.
You could supply your own comparator to your map (see any decent book on
the Standard library containers to learn how to do it), or you could just
keep the map based on 'string' and not use bare pointers.
V This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jeff Sandys |
last post by:
I'm trying to write a mapping function for genealogy.
I want to read a gedcom database and plot an icon at
a geographic location based on a user's query.
Can you help me find:
1) A python interface to gedcom?
(gedcom is a well documented linear
file so I can probably do this myself
if an interface is not available)
|
by: Stanimir Stamenkov |
last post by:
I have this kind of construct:
http://www.geocities.com/stanio/temp/usemap01.html
(an IMG element using MAP described with AREA elements)
I'm trying to make it more accessible and currently I'm testing in
Lynx. Lynx plays it clever and when I "enter" the map it shows only
3 links no matter there are 5 AREAs (3 point to a same...
|
by: Old Monk |
last post by:
Hi all,
Say I have two maps m1 and m2. Both have key as std::string and value
as long.
m1 could hold entries like
"abc" 123
"def" 456
m2 could have entries like
|
by: Sean |
last post by:
Have you ever wanted to add the great features inherent in Google Maps?
Here is how you do it.
==============
== STEP ONE ==
==============
Create a new MS Access form called frmGoogleMap. Size the form to your
liking...
|
by: John E Katich |
last post by:
Being an old MFC guy, I'm having a heck of a time working with MC++. Can
anybody tell me why the following code outputs 65 instead of an A. And yes
I know 65 is the hex value for A, but why is it getting converted? And how
to I get an A to be outputted?
char c = 'A';
StringBuilder *s = new StringBuilder;
s->Append(c);...
| |
by: Philadelphia XML User Group |
last post by:
NEXT MEETING: March 8th, 6:00 to 8:00 pm
A Definitive Introduction to Topic Maps
with Michel Biezunski and Roger Sperberg
To sign up, please visit http://www.xmlphilly.org/signup.asp
There's been lots of buzz about Topic Maps in the XML community, and
|
by: edfialk |
last post by:
Hello all, I have this web application up at http://niceguy.wustl.edu/NEISGEI/EmisComp.
It's for visually comparing emission data.
So, I added this nice loading animated gif to the maps to show users
that the image is still downloading (some data can take a couple of
minutes). I found the script somewhere on the web.
Unfortunately, it...
|
by: rn5arn5a |
last post by:
I am not sure where I should have posted this question in this
newsgroup. Please excuse me if I am wrong.
Nowadays, a lot of websites have come with Maps (Google Maps being an
example). Can someone please tell me whether these maps are created by
using graphic/imaging softwares (like Adobe PhotoShop)?
Actually one of my clients who is...
|
by: bizt |
last post by:
Hi,
I am performing an XSLT on a XML feed. The XSLT produces me with PHP
code that I eval then insert into my DB. However, the way that the
system Im assigned to is setup, I need to convert all instances of
comma ',' to entity ',' .. so this requires that the output of the
XSLT needs to produce ',' but instead it converts it to a comma...
|
by: Phil Stanton |
last post by:
I have a button on a form which when pressed displays a google map of the
address.
Code is
Private Sub Googlemap_Click()
MakeURL ("")
End Sub
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |