hash_map iterator

Discussion in 'C++' started by Charles Herman, Nov 3, 2003.

  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;

    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
     
    Charles Herman, Nov 3, 2003
    #1
    1. Advertising

  2. Charles Herman

    Mike Wahler Guest

    "Charles Herman" <> wrote in message
    news:3fa6c41a_2@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
     
    Mike Wahler, Nov 3, 2003
    #2
    1. Advertising

  3. Charles Herman

    Ron Natalie Guest

    "Charles Herman" <> wrote in message news:3fa6c41a_2@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.
     
    Ron Natalie, Nov 3, 2003
    #3
  4. 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
     
    Charles Herman, Nov 3, 2003
    #4
  5. 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.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Nov 3, 2003
    #5
  6. Charles Herman

    Ron Natalie Guest

    "Charles Herman" <> wrote in message news:3fa6d98a_2@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.
     
    Ron Natalie, Nov 4, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,436
  2. greg
    Replies:
    6
    Views:
    462
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    658
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,175
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,053
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page