By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

hash_map, Standard Template Library

P: n/a
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::operator<<
[with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((std::basic_ostream<char, std::char_traits<char>
&)(&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

Nov 7 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 2005-11-07 20:48, peter_k wrote:
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::operator<<
[with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((std::basic_ostream<char, std::char_traits<char>
&)(&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'


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
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Nov 7 '05 #2

P: n/a
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...

Nov 7 '05 #3

P: n/a
peter_k wrote:
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

Nov 7 '05 #4

P: n/a
You have help me a lot, Thanks!!

Nov 7 '05 #5

P: n/a
Jonathan Mcdougall wrote:
peter_k wrote:
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.


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.
-------------------------------------------
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.


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

Nov 8 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.