Using TCHAR* and map.h

I have a map loaded with key/elements of type TCHAR* and am trying to use the map.find() method on the map, but my elements aren't being returned. I'd had to resort to using an iterator to loop through the map and use a list of if-else statements to find the values that I was looking for, but that it a horrific way of doing so I was hoping that someone might have a better answer to this problem.

Mock-code
1
2
3
4
5
6
7
8
9
10
11
12
13
// Sudo keys and elements
TCHAR* key1 = _T("Key 1");
TCHAR* key2 = _T("Key 2");
TCHAR* element1 = _T("Element 1");
TCHAR* element2 = _T("Element 2");

// Map creation and loading of keys and elements into the map
map<TCHAR*, TCHAR*> myMap;
myMap[key1] = element1;
myMap[key2] = element2;

// Locating keys
TCHAR* foundKeyThisElement = myMap.find(_T("Key 1"))->second;


I would assume that foundKeyThisElement would have a value in place, but instead I'm getting a bad pointer. Can someone please help me understand this issue?
Last edited on
You are storing pointers, not strings. Because of this, you are storing the address of "Key 1", which is apparently different than the other "Key 1" address you are trying to use later.
Topic archived. No new replies allowed.