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. how to typedef array of 4 char to be used in map template

how to typedef array of 4 char to be used in map template

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialdata-structures
27 Posts 3 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 CPallini

    But you need the values of ESDID characters as key for the map. something like this could do the trick

    struct ESDID
    {
    char c[4];
    uint32_t operator()(){return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]); };
    };
    //..
    map m;
    //..
    syminfo s1;
    m.insert( pair(s1.symesdid(), s1) );

    "In testa che avete, Signor di Ceprano?" -- Rigoletto

    F Offline
    F Offline
    ForNow
    wrote on last edited by
    #3

    A few questions first of I am not on your level of C++ but i am getting there so my first question is anytime you code a key as a structure you need code to tell it how to handle, format, compare that key ? is that right ? second my data is big edian so DC F'10' lays out in storage 00000000 00000000 00000000 0000000A so the code shifts thngs back to little edian as int x = 10 would be A0000000 00000000 00000000 00000000 your map statement below the structure why can it not be map m;

    C 1 Reply Last reply
    0
    • F ForNow

      A few questions first of I am not on your level of C++ but i am getting there so my first question is anytime you code a key as a structure you need code to tell it how to handle, format, compare that key ? is that right ? second my data is big edian so DC F'10' lays out in storage 00000000 00000000 00000000 0000000A so the code shifts thngs back to little edian as int x = 10 would be A0000000 00000000 00000000 00000000 your map statement below the structure why can it not be map m;

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #4

      Quote:

      so my first question is anytime you code a key as a structure you need code to tell it how to handle, format, compare that key ? is that right ?

      Generally speaking, you haven't. However, in your scenario, you have to, because the key you need is the value stored inside the characters, not the reference to the struct.

      Quote:

      second my data is big edian so DC F'10' lays out in storage 00000000 00000000 00000000 0000000A so the code shifts thngs back to little edian as int x = 10 would be A0000000 00000000 00000000 00000000

      Kind of that. Note you actually don't even need the key being big-endian.

      "In testa che avete, Signor di Ceprano?" -- Rigoletto

      F 2 Replies Last reply
      0
      • C CPallini

        Quote:

        so my first question is anytime you code a key as a structure you need code to tell it how to handle, format, compare that key ? is that right ?

        Generally speaking, you haven't. However, in your scenario, you have to, because the key you need is the value stored inside the characters, not the reference to the struct.

        Quote:

        second my data is big edian so DC F'10' lays out in storage 00000000 00000000 00000000 0000000A so the code shifts thngs back to little edian as int x = 10 would be A0000000 00000000 00000000 00000000

        Kind of that. Note you actually don't even need the key being big-endian.

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #5

        Ok thank you again I am at work now I will compile it afterwards I have another map with a key made up of few elements in a structure Had compile errors but I think if I do what you just did insert code in the the structure with a for lack of a better term dummy in-line call in the structure with the operator()() {}; I may be able to straighten it out a lot of this code I can use on the z/os mainframe so I am hoping outside of a few #ifdef _MSVC_ for the big Edian to little Edian conversion it will compile with the XL C\C++ compiler it too has a STL library thank you again

        1 Reply Last reply
        0
        • C CPallini

          Quote:

          so my first question is anytime you code a key as a structure you need code to tell it how to handle, format, compare that key ? is that right ?

          Generally speaking, you haven't. However, in your scenario, you have to, because the key you need is the value stored inside the characters, not the reference to the struct.

          Quote:

          second my data is big edian so DC F'10' lays out in storage 00000000 00000000 00000000 0000000A so the code shifts thngs back to little edian as int x = 10 would be A0000000 00000000 00000000 00000000

          Kind of that. Note you actually don't even need the key being big-endian.

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          F Offline
          F Offline
          ForNow
          wrote on last edited by
          #6

          Hi I inserted the definitions you provided however I am still getting compile errors

          struct ESDID
          {
          char c[4];
          uint32_t operator()() { return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]); };
          };

          C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xstddef(127,22): error C2676: binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator
          1> with
          1> [
          1> _Ty=ESDID
          1> ]
          1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xstddef(126): message : while compiling class template member function 'bool std::less::operator ()(const _Ty &,const _Ty &) const'
          1> with
          1> [
          1> _Ty=ESDID
          1> ]
          1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xutility(1518): message : see reference to function template instantiation 'bool std::less::operator ()(const _Ty &,const _Ty &) const' being compiled
          1> with
          1> [
          1> _Ty=ESDID
          1> ]
          1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xmemory(1380): message : see reference to class template instantiation 'std::less' being compiled
          1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xmemory(1380): message : see reference to variable template 'const bool is_empty_v >' being compiled
          1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\map(75): message : see reference to class template instantiation 'std::_Tree>' being compiled
          1> with
          1> [
          1> _Kty=ESDID,
          1> _Ty=extsymbol,
          1> _Pr=std::less,
          1> _Alloc=std::allocator>
          1> ]

          here is my definition of type extsymbol not quite sure where the error is from

          struct extsymbol
          {

          unsigned char recordtype;
          unsigned char recordtypeflag;
          BYTE reserved1\[4\];
          ESDID SYMESDID;
          BYTE reserved2\[4\];
          BYTE align\[4\];
          BYTE seclen\[4\];
          ESDID sumower;
          BYTE reserved3\[8\];
          BYTE nmeoffset\[4\];
          BYTE nmelen\[4\];
          BYTE aliasoffset\[4\];
          BYTE alisnmelen;
          char symname\[63\];
          

          };

          Here is the code the references extsymbo

          C 1 Reply Last reply
          0
          • F ForNow

            Hi I inserted the definitions you provided however I am still getting compile errors

            struct ESDID
            {
            char c[4];
            uint32_t operator()() { return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]); };
            };

            C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xstddef(127,22): error C2676: binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator
            1> with
            1> [
            1> _Ty=ESDID
            1> ]
            1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xstddef(126): message : while compiling class template member function 'bool std::less::operator ()(const _Ty &,const _Ty &) const'
            1> with
            1> [
            1> _Ty=ESDID
            1> ]
            1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xutility(1518): message : see reference to function template instantiation 'bool std::less::operator ()(const _Ty &,const _Ty &) const' being compiled
            1> with
            1> [
            1> _Ty=ESDID
            1> ]
            1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xmemory(1380): message : see reference to class template instantiation 'std::less' being compiled
            1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xmemory(1380): message : see reference to variable template 'const bool is_empty_v >' being compiled
            1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\map(75): message : see reference to class template instantiation 'std::_Tree>' being compiled
            1> with
            1> [
            1> _Kty=ESDID,
            1> _Ty=extsymbol,
            1> _Pr=std::less,
            1> _Alloc=std::allocator>
            1> ]

            here is my definition of type extsymbol not quite sure where the error is from

            struct extsymbol
            {

            unsigned char recordtype;
            unsigned char recordtypeflag;
            BYTE reserved1\[4\];
            ESDID SYMESDID;
            BYTE reserved2\[4\];
            BYTE align\[4\];
            BYTE seclen\[4\];
            ESDID sumower;
            BYTE reserved3\[8\];
            BYTE nmeoffset\[4\];
            BYTE nmelen\[4\];
            BYTE aliasoffset\[4\];
            BYTE alisnmelen;
            char symname\[63\];
            

            };

            Here is the code the references extsymbo

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

            This

            Quote:

            procpointer->extsymcollector.insert({ exsympointer->SYMESDID, *exsympointer });

            should be instead

            procpointer->extsymcollector.insert({ exsympointer->SYMESDID(), *exsympointer });

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            F 1 Reply Last reply
            0
            • C CPallini

              This

              Quote:

              procpointer->extsymcollector.insert({ exsympointer->SYMESDID, *exsympointer });

              should be instead

              procpointer->extsymcollector.insert({ exsympointer->SYMESDID(), *exsympointer });

              "In testa che avete, Signor di Ceprano?" -- Rigoletto

              F Offline
              F Offline
              ForNow
              wrote on last edited by
              #8

              That gives a error of type mismatch me thinks I need a == operator inside struct esdid to tell it how to do the insert I THINK ( because ) you are the expert anytime you try to insert a type struct the insert method needs to know how to compare therefore I think compare operators I.E == , < , >

              C 1 Reply Last reply
              0
              • F ForNow

                That gives a error of type mismatch me thinks I need a == operator inside struct esdid to tell it how to do the insert I THINK ( because ) you are the expert anytime you try to insert a type struct the insert method needs to know how to compare therefore I think compare operators I.E == , < , >

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #9

                That (probably) gives type mismatch because you declared the map this way

                map

                But you shouldn't do that. Instead you should declare it like

                map

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                F 1 Reply Last reply
                0
                • C CPallini

                  That (probably) gives type mismatch because you declared the map this way

                  map

                  But you shouldn't do that. Instead you should declare it like

                  map

                  "In testa che avete, Signor di Ceprano?" -- Rigoletto

                  F Offline
                  F Offline
                  ForNow
                  wrote on last edited by
                  #10

                  now I am I am getting this

                      \]
                  

                  1>C:\SYSADATA\SYSADATA\SYSADATA\getnextsource.cpp(213,36): message : Element '1': no conversion from 'ESDID' to 'std::pair'
                  1>C:\SYSADATA\SYSADATA\SYSADATA\getnextsource.cpp(213,36): message : Element '2': no conversion from 'syminfo' to 'std::pair'
                  1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xtree(1283,10): message : see declaration of 'std::_Tree>::insert'
                  1> with
                  1> [
                  1> _Kty=uint32_t,
                  1> _Ty=syminfo,
                  1> _Pr=std::less,
                  1> _Alloc=std::allocator>
                  1> ]

                  C 1 Reply Last reply
                  0
                  • F ForNow

                    now I am I am getting this

                        \]
                    

                    1>C:\SYSADATA\SYSADATA\SYSADATA\getnextsource.cpp(213,36): message : Element '1': no conversion from 'ESDID' to 'std::pair'
                    1>C:\SYSADATA\SYSADATA\SYSADATA\getnextsource.cpp(213,36): message : Element '2': no conversion from 'syminfo' to 'std::pair'
                    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\xtree(1283,10): message : see declaration of 'std::_Tree>::insert'
                    1> with
                    1> [
                    1> _Kty=uint32_t,
                    1> _Ty=syminfo,
                    1> _Pr=std::less,
                    1> _Alloc=std::allocator>
                    1> ]

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #11

                    Did you try

                    procpointer->extsymcollector.insert(std::pair(exsympointer->SYMESDID(), *exsympointer ));

                    ?

                    "In testa che avete, Signor di Ceprano?" -- Rigoletto

                    F 1 Reply Last reply
                    0
                    • C CPallini

                      Did you try

                      procpointer->extsymcollector.insert(std::pair(exsympointer->SYMESDID(), *exsympointer ));

                      ?

                      "In testa che avete, Signor di Ceprano?" -- Rigoletto

                      F Offline
                      F Offline
                      ForNow
                      wrote on last edited by
                      #12

                      still getting type mismatch will have to look at this after work think it maybe using compare operators in the struct thank you

                      1 Reply Last reply
                      0
                      • C CPallini

                        But you need the values of ESDID characters as key for the map. something like this could do the trick

                        struct ESDID
                        {
                        char c[4];
                        uint32_t operator()(){return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]); };
                        };
                        //..
                        map m;
                        //..
                        syminfo s1;
                        m.insert( pair(s1.symesdid(), s1) );

                        "In testa che avete, Signor di Ceprano?" -- Rigoletto

                        F Offline
                        F Offline
                        ForNow
                        wrote on last edited by
                        #13

                        The complier was screaming for < operator so I give it what wants hope this works got a clean complie

                        const struct ESDID
                        {
                        char c[4];
                        uint32_t operator()() { return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]); };
                        BOOL operator< (const ESDID x) const { return c < x.c; }
                        };

                        C L 2 Replies Last reply
                        0
                        • F ForNow

                          The complier was screaming for < operator so I give it what wants hope this works got a clean complie

                          const struct ESDID
                          {
                          char c[4];
                          uint32_t operator()() { return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]); };
                          BOOL operator< (const ESDID x) const { return c < x.c; }
                          };

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #14

                          That compiles. However it is probably NOT what you want (both the key of the map and the < operator use the address of the c array). Run

                          ESDID e1, e2;
                          // init e1, e2 with the same content
                          e1.c[0] = e1.c[1] = e1.c[2] = e1.c[3] = 'A';
                          e2.c[0] = e2.c[1] = e2.c[2] = e2.c[3] = 'A';

                          cout << std::boolalpha;
                          cout << "(e1 < e2) " << (e1 < e2) << "\n";
                          cout << "(e2 < e1) " << (e2 < e1) << "\n";

                          and watch the resulting output. Can you spot the problem? You can do something like this:

                          struct ESDID
                          {
                          char c[4];
                          // overload of the cast operator
                          operator uint32_t () const { return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]); };
                          };
                          //...
                          syminfo s1;
                          map m;
                          m.insert( pair(s1.symesdid, s1) );

                          Note the key of the map uses the content of the c array.

                          "In testa che avete, Signor di Ceprano?" -- Rigoletto

                          F 1 Reply Last reply
                          0
                          • F ForNow

                            The complier was screaming for < operator so I give it what wants hope this works got a clean complie

                            const struct ESDID
                            {
                            char c[4];
                            uint32_t operator()() { return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]); };
                            BOOL operator< (const ESDID x) const { return c < x.c; }
                            };

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #15

                            Alternatively you could use a union?

                            const struct ESDID
                            {
                            union
                            {
                            char c[4];
                            uint32_t i;
                            } x;
                            bool operator< (const ESDID e) const { return x.i < e.x.i; }
                            };

                            F 1 Reply Last reply
                            0
                            • C CPallini

                              That compiles. However it is probably NOT what you want (both the key of the map and the < operator use the address of the c array). Run

                              ESDID e1, e2;
                              // init e1, e2 with the same content
                              e1.c[0] = e1.c[1] = e1.c[2] = e1.c[3] = 'A';
                              e2.c[0] = e2.c[1] = e2.c[2] = e2.c[3] = 'A';

                              cout << std::boolalpha;
                              cout << "(e1 < e2) " << (e1 < e2) << "\n";
                              cout << "(e2 < e1) " << (e2 < e1) << "\n";

                              and watch the resulting output. Can you spot the problem? You can do something like this:

                              struct ESDID
                              {
                              char c[4];
                              // overload of the cast operator
                              operator uint32_t () const { return ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]); };
                              };
                              //...
                              syminfo s1;
                              map m;
                              m.insert( pair(s1.symesdid, s1) );

                              Note the key of the map uses the content of the c array.

                              "In testa che avete, Signor di Ceprano?" -- Rigoletto

                              F Offline
                              F Offline
                              ForNow
                              wrote on last edited by
                              #16

                              I understand the compare wont work correctly however what I dont see where the cast operator () is being used> Is dynamic cast used with pair i.e <> the same as the () operator ()

                              C 1 Reply Last reply
                              0
                              • L Lost User

                                Alternatively you could use a union?

                                const struct ESDID
                                {
                                union
                                {
                                char c[4];
                                uint32_t i;
                                } x;
                                bool operator< (const ESDID e) const { return x.i < e.x.i; }
                                };

                                F Offline
                                F Offline
                                ForNow
                                wrote on last edited by
                                #17

                                thanks richard I dont see in the map documentation when using the insert method with key of type struct '<' operator must be overloaded by the user

                                C 1 Reply Last reply
                                0
                                • F ForNow

                                  thanks richard I dont see in the map documentation when using the insert method with key of type struct '<' operator must be overloaded by the user

                                  C Offline
                                  C Offline
                                  CPallini
                                  wrote on last edited by
                                  #18

                                  Actually there is (see std::map - cppreference.com[^]):

                                  template<
                                  class Key,
                                  class T,
                                  class Compare = std::less,
                                  class Allocator = std::allocator>

                                  class map;

                                  As it should be, because the std::map is a sorted container.

                                  "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                  F 1 Reply Last reply
                                  0
                                  • F ForNow

                                    I understand the compare wont work correctly however what I dont see where the cast operator () is being used> Is dynamic cast used with pair i.e <> the same as the () operator ()

                                    C Offline
                                    C Offline
                                    CPallini
                                    wrote on last edited by
                                    #19

                                    The cast is used here:

                                    pair(s1.symesdid, s1)

                                    The compiler:

                                    "Map's key must be a uint32_t, hence s1.symesdid, being an ESDID, does NOT fit. Let's see if there is a suitable cast..."

                                    "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                    1 Reply Last reply
                                    0
                                    • C CPallini

                                      Actually there is (see std::map - cppreference.com[^]):

                                      template<
                                      class Key,
                                      class T,
                                      class Compare = std::less,
                                      class Allocator = std::allocator>

                                      class map;

                                      As it should be, because the std::map is a sorted container.

                                      "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                      F Offline
                                      F Offline
                                      ForNow
                                      wrote on last edited by
                                      #20

                                      That’s not the way the map template is defined by Microsoft map Class | Microsoft Learn[^]

                                      C 1 Reply Last reply
                                      0
                                      • F ForNow

                                        That’s not the way the map template is defined by Microsoft map Class | Microsoft Learn[^]

                                        C Offline
                                        C Offline
                                        CPallini
                                        wrote on last edited by
                                        #21

                                        cppreference.com:

                                        template<
                                        class Key,
                                        class T,
                                        class Compare = std::less,
                                        class Allocator = std::allocator>

                                        class map;

                                        Microsoft:

                                        template ,
                                        class Allocator=allocator>>
                                        class map;

                                        Do you see significant differences?

                                        "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                        F 1 Reply Last reply
                                        0
                                        • C CPallini

                                          cppreference.com:

                                          template<
                                          class Key,
                                          class T,
                                          class Compare = std::less,
                                          class Allocator = std::allocator>

                                          class map;

                                          Microsoft:

                                          template ,
                                          class Allocator=allocator>>
                                          class map;

                                          Do you see significant differences?

                                          "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                          F Offline
                                          F Offline
                                          ForNow
                                          wrote on last edited by
                                          #22

                                          I see the third parameter to the template in microsoft docs is class traits in the cpp reference its class compare thank you

                                          C 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