Re: Stl map and CString trouble
Alright, I'll try to respond, but I might miss some of your points.
Quickie, that "aggregate init" was actually debug output. Here's my
watch window.
- g_VarMap [5](("AA",0x003a9e0b ""),("BB",0x003a9e1b ""),("XX",
0x003a9c95 ""),("YAR",0x003a9c96 ""),("YY",0x003a9c94 ""))
std::map<ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char>
*,std::less<ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char>
,std::allocator<std::pair<ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char>
const ,unsigned char *> > > *
+ [0] ("AA",0x003a9e0b "")
std::pair<ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char>
+ [1] ("BB",0x003a9e1b "")
std::pair<ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char>
+ [2] ("XX",0x003a9c95 "")
std::pair<ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char>
+ [3] ("YAR",0x003a9c96 "")
std::pair<ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char>
+ [4] ("YY",0x003a9c94 "")
std::pair<ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char>
+ szName " YAR"
ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char> > >
Now, if we disect this we get this:
g_VarMap [5](("AA",0x003a9e0b ""),("BB",0x003a9e1b ""),("XX",
0x003a9c95 ""),("YAR",0x003a9c96 ""),("YY",0x003a9c94 ""))
szName "YAR"
ATL::CStringT<char,ATL::StrTraitATL<char,ATL::ChTraitsCRT<char> > >
At this point in execution:
CString szName = pszVar;
szName.MakeUpper();
iter = g_VarMap->find(szName); <<Right HERE
if (iter == g_VarMap->end())
printf("Scream Now");
Now, to me, "YAR" and "YAR" are lexographically the same. But the
print does get triggered. How can I be goofing the "YAR"=="YAR" ?
"cout << g_VarMap["doh"] ;
and there was nothing at "doh" that there is now
a default value at "doh"? The [] creates a
default entry in the map if it doesn't exist.
This is why you need to either use find() or
iterate through the map.
Maybe this is what's happening? "
I encountered this (been a while since I've used an stl map), and it
is not happening. All of the values are instantiated, as you can see
from the debug output. So I've eliminated this, by trying to use
find.
I tried this simple test case just now and it worked perfectly.
// maptest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <map>
#include <atlstr.h>
using namespace std; //okay, for convenience.
void MapVars();
map<CString, int> * g_VarList;
int _tmain(int argc, _TCHAR* argv[])
{
g_VarList = new map<CString, int>();
MapVars();
map<CString, int>::const_iterator iter;
CString Name = "YAR";
iter = g_VarList->find(Name);
return 0;
}
void MapVars()
{
(*g_VarList)[("YY")] = 2;
CString Name = "YAR";
(*g_VarList)[Name] = 3;
}
So what I'm trying to do is not unreasonable. Something is really
messing things up. UNICODE is NOT defined in my project, and both my
input files that are generating the entries and requests are written
in ANSI. This is vs2008.
I'll check my casts, but I'll admit I'm a bit mystified.