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.
  • 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
                  • F ForNow

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

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

                    That's, you know, just a matter of naming... BTW, you are welcome.

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

                    F 1 Reply Last reply
                    0
                    • C CPallini

                      That's, you know, just a matter of naming... BTW, you are welcome.

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

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

                      this is just a general comment I thought MainFrame Assembler was Hard but being a C\C++ proficient coder is a very difficult skill to master as an aside just looked at the retrieval for the map class "AT" method it looks like if not found it generates an exception would of been a lot simpler if they gave back a bad return code Thank

                      L C 2 Replies Last reply
                      0
                      • F ForNow

                        this is just a general comment I thought MainFrame Assembler was Hard but being a C\C++ proficient coder is a very difficult skill to master as an aside just looked at the retrieval for the map class "AT" method it looks like if not found it generates an exception would of been a lot simpler if they gave back a bad return code Thank

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

                        It is just as easy to catch the exception. Exceptions are another useful feature of OOP languages, and provide more flexibility than simple return codes.

                        1 Reply Last reply
                        0
                        • F ForNow

                          this is just a general comment I thought MainFrame Assembler was Hard but being a C\C++ proficient coder is a very difficult skill to master as an aside just looked at the retrieval for the map class "AT" method it looks like if not found it generates an exception would of been a lot simpler if they gave back a bad return code Thank

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

                          You might use find, to the purpose (see std::map<Key,T,Compare,Allocator>::find - cppreference.com[^]).

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

                          F 1 Reply Last reply
                          0
                          • C CPallini

                            You might use find, to the purpose (see std::map<Key,T,Compare,Allocator>::find - cppreference.com[^]).

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

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

                            Thank you again you know I’m an assembler mail framer by birth when I post any question on IBMMAIN no matter how much research I have done I get screamed at wish IBMMAIN could be more like the codeproject thank. Time for to implement my code or actually yours

                            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