Re: Map problems

From: Joe Laughlin (Joseph.V.Laughlin_at_boeing.com)
Date: 06/25/04


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
computers.



Relevant Pages

  • Re: A taxonomy of types
    ... however, elsewhere in my project (off in the dynamic typesystem, ...), I ... (since I am using NULL-terminated strings), and so I have used U+10FFFF ... remember, C also has things like arrays, funtion pointers, nestable ... int RIL_TypeSmallIntP; ...
    (comp.lang.misc)
  • Re: new IL: C (sort of...).
    ... only for "recent" Pascals, ... far pointers weren't really limited, ... in my compiler, I made wchar_t a builtin type (in most cases, aliased to ... I could very well include builtin "managed strings" in the new IL. ...
    (comp.lang.misc)
  • Re: HeapFree() Failing to deallocate string
    ... I've been able to recreate and isolate the problem with HeapFree(), ... in this simplified example is just a pointer to 16 bytes to store pointers ... the szCaption strings need to be copied to the pointed to ... strings for which storage needs to be allocated, ...
    (microsoft.public.windowsce.embedded)
  • Re: Increasing efficiency in C
    ... >> The representation of a string in C is the sequence of characters, ... strings, they are passed the addresses of strings. ... supports pointers the way it does. ... Competent programmers make mistakes, too. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Would changing 'point to a' to 'point into a' twenty element array be ... > arrays of pointers is to store character strings of diverse ... comparison between what was really happening (arrays of pointers to ... pointer to a string(this probably would confuse beginners)" and ...
    (comp.lang.c)