hash_map, Standard Template Library

P

peter_k

Hi

I've defined hash_map in my code using this:
-------------------------------------------
#include <string>
#include <hash_map.h>

&

namespace __gnu_cxx {
template<>
struct hash<std::string> {
hash<char*> h;
size_t operator()(const std::string &s) const {
return h(s.c_str());
};
};
};

&

hash_map<string, string, hash<string> > words;
-------------------------------------------
I have no trouble with saving or reading data from this hash_map. But
when i'm doing something like...

hash_map<string, string, hash<string> >::iterator pointer;
pointer = words.begin();
cout << *pointer; // <-- error here :(

.... it display me compile error at the end :(

This is one of error:

103 D:\12345678\words.cpp no match for 'operator<<' in 'std::eek:perator<<
_Alloc = said:
&)(&std::cout)), ((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(&slowo)))) << wskaznik'

Thanks for the reply
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi

I've defined hash_map in my code using this:
-------------------------------------------
#include <string>
#include <hash_map.h>

&

namespace __gnu_cxx {
template<>
struct hash<std::string> {
hash<char*> h;
size_t operator()(const std::string &s) const {
return h(s.c_str());
};
};
};

&

hash_map<string, string, hash<string> > words;
-------------------------------------------
I have no trouble with saving or reading data from this hash_map. But
when i'm doing something like...

hash_map<string, string, hash<string> >::iterator pointer;
pointer = words.begin();
cout << *pointer; // <-- error here :(

... it display me compile error at the end :(

This is one of error:

103 D:\12345678\words.cpp no match for 'operator<<' in 'std::eek:perator<<

I don't understand more of that error-message than you but I would
suspect that pointer does not point to what you suspect.

Erik Wikström
 
P

peter_k

This is another error pointed to this line:

127 D:\12345678\words.cpp conversion from `std::pair<const std::string,
std::string>' to non-scalar type `std::string' requested

I'll be very glad if anyone will help me...
 
J

Jonathan Mcdougall

peter_k said:
Hi

I've defined hash_map in my code using this:
-------------------------------------------
#include <string>
#include <hash_map.h>

&

namespace __gnu_cxx {
template<>
struct hash<std::string> {
hash<char*> h;
size_t operator()(const std::string &s) const {
return h(s.c_str());
};
};
};

&

hash_map<string, string, hash<string> > words;

hash_map is non standard. Please ask in a g++ newgroup next time.
-------------------------------------------
I have no trouble with saving or reading data from this hash_map. But
when i'm doing something like...

hash_map<string, string, hash<string> >::iterator pointer;
pointer = words.begin();
cout << *pointer; // <-- error here :(

hash_map probably returns a std::pair, as std::map does. It makes
sense, an iterator points to an entry in the map and an entry is in
key=>value form.

Look in the doc for the allowed operations on a hash_map::iterator.
Very probably, you'll need to do

cout << pointer->first;

for the key and

cout << pointer->second;

for the value.


Jonathan
 
G

Greg

Jonathan said:
hash_map is non standard. Please ask in a g++ newgroup next time.

Well, STL has a hash_map, and the std::tr1 has an unordered_map; and
the type of problem being reported is possible with those or any other
hashed container.
hash_map probably returns a std::pair, as std::map does. It makes
sense, an iterator points to an entry in the map and an entry is in
key=>value form.

Look in the doc for the allowed operations on a hash_map::iterator.
Very probably, you'll need to do

cout << pointer->first;

for the key and

cout << pointer->second;

for the value.

A hash (or unordered) map stores items by a "hash", that is, an integer
value calculated from the stored item's value. As long as hash values
for different stored items are likely to be unique, the container will
be able to retrieve any of its items very quickly. It follows
therefore, that a hash map needs a function (which is called the hash
function) to calculate a hash value from the value of an item. In this
case, a hash map of std::strings needs a hash function for a
std::string.

The error being reported is that the hash map cannot find a hash
function for std::string. To fix the problem the program should
therefore define one. The easiest way to do so is to re-use the hash
function for a character pointer (const char *). Here is an example of
how this might be done for gcc's hash_map:

#include <string>

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

Note that I just put this declaration together and have not tested it.

Greg
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top