Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. TCHAR as Key in a map.

TCHAR as Key in a map.

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelpquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Comp_Users
    wrote on last edited by
    #1

    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 398

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

    L S C E 4 Replies Last reply
    0
    • C Comp_Users

      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 398

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

      L Offline
      L Offline
      liangyenchyen
      wrote on last edited by
      #2

      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;

      C 1 Reply Last reply
      0
      • L liangyenchyen

        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;

        C Offline
        C Offline
        Comp_Users
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • C Comp_Users

          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 398

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

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          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 that std::map needs, whereas a TCHAR* 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

          C 1 Reply Last reply
          0
          • C Comp_Users

            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 398

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

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            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;
            #endif

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            1 Reply Last reply
            0
            • C Comp_Users

              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 398

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

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #6

              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[^]

              1 Reply Last reply
              0
              • S Stuart Dootson

                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 that std::map needs, whereas a TCHAR* 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

                C Offline
                C Offline
                Comp_Users
                wrote on last edited by
                #7

                It works now. :) Thanks a lot for the code snippet. Thanks to others who have contributed to this thread.:thumbsup:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups