473,722 Members | 2,338 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 8617
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
9980
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
5841
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
8139
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
8861
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
8739
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
9383
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9236
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...
0
8051
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
6681
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
5995
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
4502
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...
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.