Re: Map problems
From: Joe Laughlin (Joseph.V.Laughlin_at_boeing.com)
Date: Thu, 24 Jun 2004 23:13:12 GMT
Joe Laughlin wrote:
> John Harrison wrote:
>> "Joe Laughlin" <Joseph.V.Laughlin@boeing.com> wrote in
>> message news:Hzu0xA.KFy@news.boeing.com...
>>> I have this snippet of code. Let me know if I need to
>>> give more for feedback:
>>> typedef std::map<const char*, float> AttitudeRecord;
>>> // player_info.GetAttitude should return an
>>> AttitudeRecord with // key: roll value: 34
>>> // key: pitch value: 598
>>> // key: attitude value: 34
>>> AttitudeRecord atttiude = player_info.GetAttitude();
>>> AttitudeRecord::iterator iter;
>>> iter = attitude.find("pitch");
>>> // This prints out a completely wrong value! Why?
>>> std::cout << "Value of pitch: " << attitude->second;
>> Presumably you mean iter->second.
> Doh, I did.
>> The problem is that you have a map whose keys are
>> pointers. So lookup is done by comparing pointers, not by
>> comparing strings, which I think is what you are
>> assuming. Presumably in this case the pointer you use to
>> lookup the record is not *precisely* the same pointer you
>> used to add the record.
>> Simple answer is to use real strings
>> typedef std::map<std::string, float> AttitudeRecord;
> I'll try that. I was just going off some examples I
> found, and they used const char*. When I first started,
> I used std::string, but I ran into some troubles that
> using const char* worked instead.
Doh, my problems were that I didn't include the string header. I hate