473,323 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

using maps of string *.

Hi all,

I'm trying to use a map of string* and unsigned ints but results are
not good. I've used as example this piece of code:
http://www.sgi.com/tech/stl/Map.html

And I have done this:
#include <iostream>
#include <map>

using namespace std;

struct ltstr {
bool operator()(const string * s1, const string * s2) const {
return *s1 == *s2;
}
};

int main() {

map<const string *, unsigned int, ltstr> m;
const string * s1 = new string("Hello");
const string * s2 = new string("Buh");

m[s1] = 3;
m[s2] = 4;

const string * s4 = new string("Hello");

cout << "S4 VALUE: " << m[s4] << "\n";

delete s1;
delete s2;
delete s4;

return 0;
}
For some reason I get:
$ ./maptest
S4 VALUE: 0

Can't understand what's happening. I was expecting value 3. Shouldn't
*s1 == *s2 return true iff s1 and s2 are lexicographically equal?

Cheers,

Paulo Matos

Jul 23 '05 #1
2 1298
pmatos wrote:
Can't understand what's happening. I was expecting value 3. Shouldn't
*s1 == *s2 return true iff s1 and s2 are lexicographically equal?


Yes, but this is not what map wants from you.
map wants a sorting predicate which represents 'less then'!
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2


Karl Heinz Buchegger wrote:
pmatos wrote:
Can't understand what's happening. I was expecting value 3. Shouldn't
*s1 == *s2 return true iff s1 and s2 are lexicographically equal?


Yes, but this is not what map wants from you.
map wants a sorting predicate which represents 'less then'!


Ah, how stupid of me. :) Eh, I just had to substitute *s1 == *s2 by *s1
< *s2. :) LOL Thanks. :D

--
Karl Heinz Buchegger
kb******@gascad.at


Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Old Monk | last post by:
Hi all, Say I have two maps m1 and m2. Both have key as std::string and value as long. m1 could hold entries like "abc" 123 "def" 456 m2 could have entries like
3
by: Sean | last post by:
Have you ever wanted to add the great features inherent in Google Maps? Here is how you do it. ============== == STEP ONE == ============== Create a new MS Access form called frmGoogleMap....
0
by: Fraser Dickson | last post by:
I am building a web based system using ASP.NET and VB.NET which has to interact with a web service which uses XML WDDX packets. I have been given the XML Packet Specification by the Web Service...
6
by: pagina21 | last post by:
I am a true beginner in Access and coding. I am creating a database with facility information and I want to use the address of a specific facility to display a map on my form or at least open a...
3
by: Phil Stanton | last post by:
I have a button on a form which when pressed displays a google map of the address. Code is Private Sub Googlemap_Click() MakeURL ("") End Sub
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.