473,803 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

hash_map iterator

I have the folowing program:
#include <iostream>
#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

typedef hash_map<char*, long, hash<char*> > char_hash_t;
typedef hash_map<char*, long, hash<char*> >::value_type char_pair_t;

int main()
{
char key[100];
char_hash_t ht;

for (long i = 0; i < 10; ++i)
{
sprintf (key, "%lld", 23*i);
ht.insert( char_pair_t ( key, i ));
}

char_hash_t::it erator p;
for (p = ht.begin(); p != ht.end(); ++p)
cout << "key = " << p->first
<< " val = " << p->second << endl;

return 0;
}

When I run it, it prints the line

key = 207 val = 7

then goes into an infinite loop printing out the line

key = 207 val = 8

until I hit ctrl-c.

Can anyone see a problem with it? It looks perfectly good to me.

-charles

Jul 19 '05 #1
5 20623
"Charles Herman" <sp**@no.spam > wrote in message
news:3f******** @127.0.0.1...
I have the folowing program:
#include <iostream>
#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

typedef hash_map<char*, long, hash<char*> > char_hash_t;
typedef hash_map<char*, long, hash<char*> >::value_type char_pair_t;
int main()
{
char key[100];
char_hash_t ht;
'hash_map' is not part of standard C++, so I can't comment on it, but...

for (long i = 0; i < 10; ++i)
{
sprintf (key, "%lld", 23*i);
The correct specifier for 'long' is %ld, not %lld. Perhaps this
is the trouble.

More below.

ht.insert( char_pair_t ( key, i ));
}

char_hash_t::it erator p;
for (p = ht.begin(); p != ht.end(); ++p)
cout << "key = " << p->first
<< " val = " << p->second << endl;

return 0;
}

When I run it, it prints the line

key = 207 val = 7

then goes into an infinite loop printing out the line

key = 207 val = 8

until I hit ctrl-c.

Can anyone see a problem with it? It looks perfectly good to me.


(it seems to me that from your logic, a 'key'
of 207 should have a 'value' of 9 (207 / 23). I suppose an
invalid printf() format specifier could generate such bogus values,
which might cause your 'for' loop's iteration to 'step over' the
value specified by its conditional expression.

OF course all this is just speculation, since an invalid printf()
format specifier results in 'undefined behavior'.

HTH,

-Mike
Jul 19 '05 #2

"Charles Herman" <sp**@no.spam > wrote in message news:3f******** @127.0.0.1...
char key[100];
char_hash_t ht;

for (long i = 0; i < 10; ++i)
Why are you using long?
{
sprintf (key, "%lld", 23*i);
ht.insert( char_pair_t ( key, i ));
}


You are inserting ten items with the same value "(char*) key".
char* is a pointer value (which never changes over your loop).
It is NOT a string type.

You'd get what you wanted if you had defined the using std::string.

hash_map by the way is NOT part of the C++ standard.
Jul 19 '05 #3
Charles Herman wrote:
I have the folowing program:
#include <iostream>
#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

typedef hash_map<char*, long, hash<char*> > char_hash_t;
typedef hash_map<char*, long, hash<char*> >::value_type
char_pair_t;

int main()
{
char key[100];
char_hash_t ht;

for (long i = 0; i < 10; ++i)
{
sprintf (key, "%lld", 23*i);
ht.insert( char_pair_t ( key, i ));
}

char_hash_t::it erator p;
for (p = ht.begin(); p != ht.end(); ++p)
cout << "key = " << p->first
<< " val = " << p->second << endl;

return 0;
}

When I run it, it prints the line

key = 207 val = 7

then goes into an infinite loop printing out the line

key = 207 val = 8

until I hit ctrl-c.

Can anyone see a problem with it? It looks perfectly good to me.

-charles


Thanks to everyone for answering my post.

It turns out the fact that I used char key[100] was the problem (thanks to
Ron for pointing this out). When I used new to allocate new memory for
each key inside the loop, everything works well. Off course, this raises
the problem of deleting the memory when I am through with the hash map. I
think I will use ostringstream to create the keys. But will this memeory
actually be freed when i am finsished with the hash map?

There were severalissues raised by the repondents. I cannot use std::string
because hash_map is not overloaded to accept string. So I could use string
(which is what I would end up with after using ostringstream) and then use
c_str() to extract the char, or I could use the method I used.

Contrary to your and my intuition, "%lld" is the correct specifier for a
long int, not "%ld" - this is using g++.

I am using long becaue my problem domain demands it.

-charles

Jul 19 '05 #4
Charles Herman escribió:
There were severalissues raised by the repondents. I cannot use std::string
because hash_map is not overloaded to accept string. So I could use string
(which is what I would end up with after using ostringstream) and then use
c_str() to extract the char, or I could use the method I used.
You need only to write an specialization of hash for strings. A simple
way is use the hash for char *, something like:

template <> struct hash <std::string>
{
hash () : hashstr (hash <const char *> () )
{ }
size_t operator () (const std::string & str) const
{
return hashstr (str.c_str () );
}
private:
hash <const char *> hashstr;
};

You may need to modify this depending on your hash_map implementation.
Contrary to your and my intuition, "%lld" is the correct specifier for a
long int, not "%ld" - this is using g++.


l is for long int, ll is for long long int. See man 3 printf

Regards.
Jul 19 '05 #5

"Charles Herman" <sp**@no.spam > wrote in message news:3f******** @127.0.0.1...

There were severalissues raised by the repondents. I cannot use std::string
because hash_map is not overloaded to accept string. So I could use string
(which is what I would end up with after using ostringstream) and then use
c_str() to extract the char, or I could use the method I used.
The hash_map doesn't need to be overloaded. It's the hash function that
the STL only provides the char* overload.

You could always write
template <> class hash<std::strin g> {
public:
size_T operator(const std::string& h) {
return hash<const char*>(h.c_str( ));
}
};
Contrary to your and my intuition, "%lld" is the correct specifier for a
long int, not "%ld" - this is using g++.
Contrary to your understanding, %lld IS for "long long int." The fact that
it works for you is purely coincidental.
I am using long becaue my problem domain demands it.


Not as you've expressed it here.
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2427
by: Kristofer Pettijohn | last post by:
Greetings, I have a 2D hash_map, defined as follows: typedef hash_map<const char*, str_session, hash<const char*>, eqstr> t_hashchar; typedef hash_map<const char*, t_hashchar,
3
11881
by: Mark | last post by:
Hi, I'm trying to use hash_map (gcc 3.2.2) with a std::string as the key. It will compile if I use <map> but I get a bunch of template compile errors when I change it to hash_map. Any suggestions? My program and the errors are below... #include <ext/hash_map> #include <string>
5
8625
by: peter_k | last post by:
Hi I've defined hash_map in my code using this: ------------------------------------------- #include <string> #include <hash_map.h> & namespace __gnu_cxx {
1
2975
by: anjangoswami06 | last post by:
Hi, I am interested to know how it is possible to update the data type with "insert" with stl hash_map. Suppose we have, hash_map<const char *, MyDataType, hash_compare<const char*, some_compare_func_obj>> x; //I want to insert a new instance of MyDataType if a new key is encountered but if it is a key //which already exists I want to update
3
3844
by: kony | last post by:
Hi there, I would much appreciate your help with the following problem. Below is the code that uses a hash_map. I want to release all the memory occupied by the hash_map for other use. Apparently clear() function is not working and the trick with swap() is half working. Does anybody know how to deallocate the hash_map? Thanks in advance. Kon #include <functional>
2
4284
by: Amit Bhatia | last post by:
Hi, I am trying to use hash maps from STL on gcc 3.3 as follows: #ifndef NODE_H #define NODE_H #include <ext/hash_map> #include "node_hasher.h" class Node; typedef hash_map<pair<int,int>, Node, Node_HasherLoc_Tree;
6
3039
by: Paul Bilokon | last post by:
Hi, Is there a more elegant and/or efficient way of saving the keys from a hash_map in a vector? Perhaps using assign or other algorithms? The problem, of course, is that we are dealing with an iterator of pairs... hash_map<string, void *> hashMap; // ... vector<string> keys; keys.reserve(hashMap.size());
5
3376
by: frankw | last post by:
Hi, I have a hash_map with string as key and an object pointer as value. the object is like class{ public: float a; float b; ...
2
8152
by: marek.vondrak | last post by:
Hi, I am wondering if there are any functional differences between SGI's hash_map and tr1's unordered_map. Can these two containers be interchanged? What would it take to switch from hash_map to unordered_map? Thank you. -Marek
0
10546
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9121
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6841
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5498
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.