TCHAR as Key in a map.
-
Hi, In the application that I am developing, I wants to store a list(using map) of structures. Here I want the key to be an array of wide characters i.e of type TCHAR.
typedef struct
{
int nID;
int somedata;
}MyStruct;The structure list would be of the form.
typedef std::map MyStructMap;
MyStructMap objStructMap;I want to filling the map using a key of type TCHAR which will uniquely identify each instance of the struct that is inserted. The place where I am inserting an structure with an unique TCHAR key is as follows.
Mystruct structobj1;
structobj1.nID = 100;
structobj2.somedata = 253;objStructMap.insert(MyStructMap::value_type(_T("Unique1"), structobj1)).second;
//Do the above line multiple number of times for different keys and struct object values.The Insert operation mentioned above is failing with error
error C2665: 'std::pair<_Ty1,_Ty2>::pair' : none of the 3 overloads could convert all the argument types e:\vc05\sam\MyApp\MyApp.cpp 398
error C2228: left of '.second' must have class/struct/union e:\vc05\sam\MyApp\MyApp.cpp 398I could you an pointer to a TCHAR as a Key for the map, but don't want to do that because I have to create an object on the heap and dont want to do that. Is my technique of inseting an TCHAR array and struct pair in the map object briefed above correct? Please point the cause of the error.
-
Hi, In the application that I am developing, I wants to store a list(using map) of structures. Here I want the key to be an array of wide characters i.e of type TCHAR.
typedef struct
{
int nID;
int somedata;
}MyStruct;The structure list would be of the form.
typedef std::map MyStructMap;
MyStructMap objStructMap;I want to filling the map using a key of type TCHAR which will uniquely identify each instance of the struct that is inserted. The place where I am inserting an structure with an unique TCHAR key is as follows.
Mystruct structobj1;
structobj1.nID = 100;
structobj2.somedata = 253;objStructMap.insert(MyStructMap::value_type(_T("Unique1"), structobj1)).second;
//Do the above line multiple number of times for different keys and struct object values.The Insert operation mentioned above is failing with error
error C2665: 'std::pair<_Ty1,_Ty2>::pair' : none of the 3 overloads could convert all the argument types e:\vc05\sam\MyApp\MyApp.cpp 398
error C2228: left of '.second' must have class/struct/union e:\vc05\sam\MyApp\MyApp.cpp 398I could you an pointer to a TCHAR as a Key for the map, but don't want to do that because I have to create an object on the heap and dont want to do that. Is my technique of inseting an TCHAR array and struct pair in the map object briefed above correct? Please point the cause of the error.
struct str { char cFileName[247]; }; struct cmp_str { bool operator()(str const a, str const b) const { return strcmpi(a.cFileName, b.cFileName) < 0; } }; typedef std::map<str,> GetFileMap; typedef GetFileMap::value_type GetFileValType; typedef GetFileMap::iterator GetFileItor;
-
struct str { char cFileName[247]; }; struct cmp_str { bool operator()(str const a, str const b) const { return strcmpi(a.cFileName, b.cFileName) < 0; } }; typedef std::map<str,> GetFileMap; typedef GetFileMap::value_type GetFileValType; typedef GetFileMap::iterator GetFileItor;
liangyenchyen, I am not able to get you. All i want to achieve is add my user defined structure and associate a key which is an TCHAR array. some thing like objStructMap.insert(MyStructMap::value_type(_T("Unique1"), structobj1)).second; objStructMap.insert(MyStructMap::value_type(_T("Unique2"), structobj2)).second; objStructMap.insert(MyStructMap::value_type(_T("Unique3"), structobj3)).second; and so on. Later I can access a member using the key as follows MyStructMap::iterator iter = objStructMap.find(_T("Unique1")); How do i achieve this?
-
Hi, In the application that I am developing, I wants to store a list(using map) of structures. Here I want the key to be an array of wide characters i.e of type TCHAR.
typedef struct
{
int nID;
int somedata;
}MyStruct;The structure list would be of the form.
typedef std::map MyStructMap;
MyStructMap objStructMap;I want to filling the map using a key of type TCHAR which will uniquely identify each instance of the struct that is inserted. The place where I am inserting an structure with an unique TCHAR key is as follows.
Mystruct structobj1;
structobj1.nID = 100;
structobj2.somedata = 253;objStructMap.insert(MyStructMap::value_type(_T("Unique1"), structobj1)).second;
//Do the above line multiple number of times for different keys and struct object values.The Insert operation mentioned above is failing with error
error C2665: 'std::pair<_Ty1,_Ty2>::pair' : none of the 3 overloads could convert all the argument types e:\vc05\sam\MyApp\MyApp.cpp 398
error C2228: left of '.second' must have class/struct/union e:\vc05\sam\MyApp\MyApp.cpp 398I could you an pointer to a TCHAR as a Key for the map, but don't want to do that because I have to create an object on the heap and dont want to do that. Is my technique of inseting an TCHAR array and struct pair in the map object briefed above correct? Please point the cause of the error.
You are using pointers to TCHAR as the string, in effect, not arrays. Arrays are funny things in C and C++ - they're really just pointers in drag. Anyway, you're better off using a string class, like
std::basic_string<TCHAR>
, because it already defines comparison operations thatstd::map
needs, whereas aTCHAR*
doesn't. Something like this should work:typedef std::basic_string<TCHAR> tstring;
typedef std::map<tstring, MyStruct> MyStructMap;
MyStructMap objStructMap;objStructMap.insert(std::make_pair(_T("Unique1"), structobj1));
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Hi, In the application that I am developing, I wants to store a list(using map) of structures. Here I want the key to be an array of wide characters i.e of type TCHAR.
typedef struct
{
int nID;
int somedata;
}MyStruct;The structure list would be of the form.
typedef std::map MyStructMap;
MyStructMap objStructMap;I want to filling the map using a key of type TCHAR which will uniquely identify each instance of the struct that is inserted. The place where I am inserting an structure with an unique TCHAR key is as follows.
Mystruct structobj1;
structobj1.nID = 100;
structobj2.somedata = 253;objStructMap.insert(MyStructMap::value_type(_T("Unique1"), structobj1)).second;
//Do the above line multiple number of times for different keys and struct object values.The Insert operation mentioned above is failing with error
error C2665: 'std::pair<_Ty1,_Ty2>::pair' : none of the 3 overloads could convert all the argument types e:\vc05\sam\MyApp\MyApp.cpp 398
error C2228: left of '.second' must have class/struct/union e:\vc05\sam\MyApp\MyApp.cpp 398I could you an pointer to a TCHAR as a Key for the map, but don't want to do that because I have to create an object on the heap and dont want to do that. Is my technique of inseting an TCHAR array and struct pair in the map object briefed above correct? Please point the cause of the error.
First, you have to insert the < and > symbols yourself using the buttons just above the emoticons, oyherwise they will disappear. Next, why don't you use a std::string or std::wstring to do that ? It will make your life way much easier. The map will rely on fact that the objects you provide as a key are comparable, which is not the case with C strings (what will be compared is the pointer address, not the contents). Thus this will simply not work. The other reply you got was simply to say that you can get over it by providing your own structure which overrides the == operator. But, it is making your life complicated. If I were you, I would simply use the std::string (in non unicode build) or std::wstring (in UNICODE build) for that. If you have to support both builds, then simply define your own type:
#if defined _UNICODE || defined UNICODE
typedef std::wstring TMyString;
#else
typedef std::string TMyString;
#endifCédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Hi, In the application that I am developing, I wants to store a list(using map) of structures. Here I want the key to be an array of wide characters i.e of type TCHAR.
typedef struct
{
int nID;
int somedata;
}MyStruct;The structure list would be of the form.
typedef std::map MyStructMap;
MyStructMap objStructMap;I want to filling the map using a key of type TCHAR which will uniquely identify each instance of the struct that is inserted. The place where I am inserting an structure with an unique TCHAR key is as follows.
Mystruct structobj1;
structobj1.nID = 100;
structobj2.somedata = 253;objStructMap.insert(MyStructMap::value_type(_T("Unique1"), structobj1)).second;
//Do the above line multiple number of times for different keys and struct object values.The Insert operation mentioned above is failing with error
error C2665: 'std::pair<_Ty1,_Ty2>::pair' : none of the 3 overloads could convert all the argument types e:\vc05\sam\MyApp\MyApp.cpp 398
error C2228: left of '.second' must have class/struct/union e:\vc05\sam\MyApp\MyApp.cpp 398I could you an pointer to a TCHAR as a Key for the map, but don't want to do that because I have to create an object on the heap and dont want to do that. Is my technique of inseting an TCHAR array and struct pair in the map object briefed above correct? Please point the cause of the error.
To simply make it work,
std::map<const TCHAR\*,MyStruct> myMap; MyStruct obj; myMap.insert(std::make\_pair(\_T("ONE"),obj));
Hey but look that's a TCHAR pointer, In general you should be careful while using pointers in containers, if scope goes out, you will get a bang. What I've suggested is not a solution, it's just to get your code compile. As Cedric has said, std strings gets along well with maps.
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
-
You are using pointers to TCHAR as the string, in effect, not arrays. Arrays are funny things in C and C++ - they're really just pointers in drag. Anyway, you're better off using a string class, like
std::basic_string<TCHAR>
, because it already defines comparison operations thatstd::map
needs, whereas aTCHAR*
doesn't. Something like this should work:typedef std::basic_string<TCHAR> tstring;
typedef std::map<tstring, MyStruct> MyStructMap;
MyStructMap objStructMap;objStructMap.insert(std::make_pair(_T("Unique1"), structobj1));
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
It works now. :) Thanks a lot for the code snippet. Thanks to others who have contributed to this thread.:thumbsup: