hash_map, Standard Template Library | | |
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>[color=blue]
>&)(&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'[/color]
Thanks for the reply | | | | re: hash_map, Standard Template Library
On 2005-11-07 20:48, peter_k wrote:[color=blue]
> 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>[color=green]
>>&)(&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'[/color][/color]
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 | | | | re: hash_map, Standard Template Library
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... | | | | re: hash_map, Standard Template Library
peter_k wrote:[color=blue]
> 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;[/color]
hash_map is non standard. Please ask in a g++ newgroup next time.
[color=blue]
> -------------------------------------------
> 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 :([/color]
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 | | | | re: hash_map, Standard Template Library
You have help me a lot, Thanks!! | | | | re: hash_map, Standard Template Library
Jonathan Mcdougall wrote:[color=blue]
> peter_k wrote:[color=green]
> > 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;[/color]
>
> hash_map is non standard. Please ask in a g++ newgroup next time.[/color]
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.
[color=blue][color=green]
> > -------------------------------------------
> > 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 :([/color]
>
> 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.[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|