473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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::iterator 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 20598
"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::iterator 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::iterator 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::string> {
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
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
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...
5
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
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*,...
3
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...
2
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>,...
6
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...
5
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.