By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

using maps of string *.

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a


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 discussion thread is closed

Replies have been disabled for this discussion.