473,803 Members | 3,306 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::strin g> {
hash<char*> h;
size_t operator()(cons t 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\wor ds.cpp no match for 'operator<<' in 'std::operator< <
[with _CharT = char, _Traits = std::char_trait s<char>, _Alloc =
std::allocator< char>](((std::basic_o stream<char, std::char_trait s<char>
&)(&std::cout) ), ((const std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> >&)((const std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> >*)(&slowo))) ) << wskaznik'


Thanks for the reply

Nov 7 '05 #1
5 8625
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::strin g> {
hash<char*> h;
size_t operator()(cons t 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\wor ds.cpp no match for 'operator<<' in 'std::operator< <
[with _CharT = char, _Traits = std::char_trait s<char>, _Alloc =
std::allocator< char>](((std::basic_o stream<char, std::char_trait s<char>
&)(&std::cout )), ((const std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> >&)((const std::basic_stri ng<char, std::char_trait s<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
This is another error pointed to this line:

127 D:\12345678\wor ds.cpp conversion from `std::pair<cons t 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
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::strin g> {
hash<char*> h;
size_t operator()(cons t 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::itera tor.
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
You have help me a lot, Thanks!!

Nov 7 '05 #5
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::strin g> {
hash<char*> h;
size_t operator()(cons t 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::itera tor.
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
9988
by: Florian Liefers | last post by:
"Hello World\n", i get error C2143 (Syntaxerror, missing ';' before '<') using the following code: #include <hash_map> struct eqstr { bool operator()(const char* s1, const char* s2) const
3
11881
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 suggestions? My program and the errors are below... #include <ext/hash_map> #include <string>
4
5847
by: Christian Meier | last post by:
Hello, Yes, I know that hash maps aren't in the standard. But I couldn't find any better newsgroup for this post. (or is there an SGI library newsgroup?) I am currently testing the hash_map implementation of SGI. And now I am not sure if it is really true, what I discovered.... Here is my code:
4
4913
by: yuyang08 | last post by:
Hello, everyone, I am wondering what is the hash function that is used in hash_map/hash_set. Can I replace it with my own hash function? Any comments on this? Thanks! -Andy
11
1228
by: aaragon | last post by:
Hello everyone, I have a VERY BIG set of double values that I want to map to intervals so I thought a clever way to do this was using a hash table. Let's say that I want to map all double values in the range 0-0.5 to a single std::pair<double,double>. This is what I've done so far: #include <iostream>
1
1777
by: Axel Gallus | last post by:
Hello, i have a question concerning STL non-standard hash_maps under Visual Studio 2005: Microsoft STL requires a "hash_compare" object for hash_maps: template <class Key, class Type, class Traits=hash_compare<Key, less<Key, class Allocator=allocator<pair <const Key, Type > class hash_map
1
475
by: Mirco Wahab | last post by:
Alf P. Steinbach wrote: No, there isn't any (afaik). You can look it up here: http://www.boost.org/doc/libs/1_35_0/doc/html/boost_tr1/unsupported.html#boost_tr1.unsupported.unordered_map Thanks & regards M.
5
3376
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
8152
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 unordered_map? Thank you. -Marek
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10292
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.