473,729 Members | 2,272 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 8618
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
9981
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
11879
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
5843
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
4909
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
3371
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
8141
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
8917
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
9281
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
9200
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
9142
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...
1
6722
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...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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

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.