struct in map

Discussion in 'C++' started by Ron Eggler, May 30, 2008.

  1. Ron Eggler

    Ron Eggler Guest

    Hi,

    I would like to do something like this:

    typedef struct{
    unsigned short msg;
    int SeqNr;
    } MsgStruct;
    map <string, MsgStruct> MyMsg;

    Now I'm wondering how this is compatible with the standard and how I would
    access the structs variables directly?
    Would it be something like this:
    MyMsg["index"].msg= 0xabcd;?
    I'm not 100% sure if this would work and i don't have a compiler handy right
    now to test this.

    Thanks for your opinion,
    Ron
    --
    weeks of software enineering safe hours of planing ;)
    Ron Eggler, May 30, 2008
    #1
    1. Advertising

  2. Ron Eggler

    Eric Pruneau Guest

    "Ron Eggler" <> a écrit dans le message de news:
    K4X%j.3$Gn.1@edtnps92...
    > Hi,
    >
    > I would like to do something like this:
    >
    > typedef struct{
    > unsigned short msg;
    > int SeqNr;
    > } MsgStruct;
    > map <string, MsgStruct> MyMsg;
    >
    > Now I'm wondering how this is compatible with the standard and how I would
    > access the structs variables directly?
    > Would it be something like this:
    > MyMsg["index"].msg= 0xabcd;?
    > I'm not 100% sure if this would work and i don't have a compiler handy
    > right
    > now to test this.


    You could always use an online compiler like this one
    http://www.comeaucomputing.com/tryitout/
    to test your code.
    Eric Pruneau, May 30, 2008
    #2
    1. Advertising

  3. Ron Eggler

    Guest

    On May 30, 1:16 pm, "Eric Pruneau" <> wrote:
    > "Ron Eggler" <> a écrit dans le message de news:
    > K4X%j.3$Gn.1@edtnps92...
    >
    >
    >
    >
    >
    > > Hi,

    >
    > > I would like to do something like this:

    >
    > >        typedef struct{
    > >                unsigned short msg;
    > >                int SeqNr;
    > >        } MsgStruct;
    > >        map <string, MsgStruct> MyMsg;

    >
    > > Now I'm wondering how this is compatible with the standard and how I would
    > > access the structs variables directly?
    > > Would it be something like this:
    > > MyMsg["index"].msg= 0xabcd;?
    > > I'm not 100% sure if this would work and i don't have a compiler handy
    > > right
    > > now to test this.

    >
    > You could always use an online compiler like this onehttp://www.comeaucomputing.com/tryitout/



    I'm not aware of any others that are available online. I would have
    thought there would be some effort to move in that direction a long
    time ago. Comeau took a baby step but hasn't expanded on that in
    years. Just to give Comeau a little hint, adding https support
    could be considered. We're working on that and G-d willing it
    will be available next month - June.


    Brian Wood
    Ebenezer Enterprises
    www.webEbenezer.net
    to expand on that.
    , May 30, 2008
    #3
  4. Ron Eggler

    Ron Eggler Guest

    Eric Pruneau wrote:

    >
    > "Ron Eggler" <> a écrit dans le message de news:
    > K4X%j.3$Gn.1@edtnps92...
    >> Hi,
    >>
    >> I would like to do something like this:
    >>
    >> typedef struct{
    >> unsigned short msg;
    >> int SeqNr;
    >> } MsgStruct;
    >> map <string, MsgStruct> MyMsg;
    >>
    >> Now I'm wondering how this is compatible with the standard and how I
    >> would access the structs variables directly?
    >> Would it be something like this:
    >> MyMsg["index"].msg= 0xabcd;?
    >> I'm not 100% sure if this would work and i don't have a compiler handy
    >> right
    >> now to test this.

    >
    > You could always use an online compiler like this one
    > http://www.comeaucomputing.com/tryitout/

    Wow, this is cool! Yeah, I like it! Great tool! :)
    Thanks!
    Got my code compiled properly like this:
    [C++]
    #include <map>
    #include <string>
    using namespace std;

    struct MsgStruct{
    unsigned short msg;
    int SeqNr;
    };

    int main (void){
    map <string, MsgStruct> MyMsg;

    MyMsg["index"].SeqNr=0x1234;

    }


    [/C++]
    Should work fine then i guess :)
    Thanks for everybody's help! Good stuff! :)
    > to test your code.


    --
    weeks of software enineering safe hours of planing ;)
    Ron Eggler, May 30, 2008
    #4
  5. Ron Eggler

    Jim Langston Guest

    Ron Eggler wrote:
    > Hi,
    >
    > I would like to do something like this:
    >
    > typedef struct{
    > unsigned short msg;
    > int SeqNr;
    > } MsgStruct;
    > map <string, MsgStruct> MyMsg;
    >
    > Now I'm wondering how this is compatible with the standard and how I
    > would access the structs variables directly?
    > Would it be something like this:
    > MyMsg["index"].msg= 0xabcd;?
    > I'm not 100% sure if this would work and i don't have a compiler
    > handy right now to test this.


    Yes. For a map MyMas{key] returns a reference to the item, creating it if
    it doesn't already exist. So
    MyMas["index"].msg
    referes to the unsigned short to the unsigned short in the instance of
    MsgStruct that is the entry for "index".

    Note that this may, or may not, be what you want. It depends if you want
    the instance created for the key if it doesn't exist, or not.

    Also, as others have noted, the C++ way to create the structure is:

    struct MsgStruct {
    unsigned short msg;
    int SeqNr;
    };



    --
    Jim Langston
    Jim Langston, May 31, 2008
    #5
  6. Ron Eggler

    Daniel T. Guest

    "Victor Bazarov" <> wrote:
    > Daniel T. wrote:
    > > Ron Eggler <> wrote:

    >
    > >> I would like to do something like this:

    >
    > >>         typedef struct{
    > >>                 unsigned short msg;
    > >>                 int SeqNr;
    > >>         } MsgStruct;
    > >>         map <string, MsgStruct> MyMsg;

    >
    > >> Now I'm wondering how this is compatible with the standard and how I
    > >> would access the structs variables directly?
    > >> Would it be something like this:
    > >> MyMsg["index"].msg= 0xabcd;?
    > >> I'm not 100% sure if this would work and i don't have a compiler
    > >> handy right now to test this.

    >
    > > In addition to the information you have gotten so far, I strongly
    > > suggest you make a default constructor to initialize the variables.
    > > Otherwise when you type:

    >
    > > MyMsg["index"].msg = 0xabcd;

    >
    > > you have no idea what MyMsg["index"].SeqNr equals.

    >
    > Well, maybe he has no idea, maybe somebody else has no idea, but the
    > Standard actuall says that the struct is value-initialised, which
    > means that right before the assignment both values are 0.  Therefore
    > there is no need in a default c-tor.


    So given the below:

    struct foo {
    int bar1;
    };

    struct other_foo {
    foo itsFoo;
    other_foo():itsFoo() { }
    };

    int main() {
    other_foo obj;
    assert(obj.itsFoo.bar1 == 0);
    }

    You are saying that the above assert is guaranteed never to fire? I'd
    be surprised if that was true because un-initialized variables have
    been a huge issue in just about every project I've ever worked on.
    Daniel T., May 31, 2008
    #6
  7. On 2008-05-31 21:27, Daniel T. wrote:
    > "Victor Bazarov" <> wrote:
    >> Daniel T. wrote:
    >> > Ron Eggler <> wrote:

    >>
    >> >> I would like to do something like this:

    >>
    >> >> typedef struct{
    >> >> unsigned short msg;
    >> >> int SeqNr;
    >> >> } MsgStruct;
    >> >> map <string, MsgStruct> MyMsg;

    >>
    >> >> Now I'm wondering how this is compatible with the standard and how I
    >> >> would access the structs variables directly?
    >> >> Would it be something like this:
    >> >> MyMsg["index"].msg= 0xabcd;?
    >> >> I'm not 100% sure if this would work and i don't have a compiler
    >> >> handy right now to test this.

    >>
    >> > In addition to the information you have gotten so far, I strongly
    >> > suggest you make a default constructor to initialize the variables.
    >> > Otherwise when you type:

    >>
    >> > MyMsg["index"].msg = 0xabcd;

    >>
    >> > you have no idea what MyMsg["index"].SeqNr equals.

    >>
    >> Well, maybe he has no idea, maybe somebody else has no idea, but the
    >> Standard actuall says that the struct is value-initialised, which
    >> means that right before the assignment both values are 0. Therefore
    >> there is no need in a default c-tor.

    >
    > So given the below:
    >
    > struct foo {
    > int bar1;
    > };
    >
    > struct other_foo {
    > foo itsFoo;
    > other_foo():itsFoo() { }
    > };
    >
    > int main() {
    > other_foo obj;
    > assert(obj.itsFoo.bar1 == 0);
    > }
    >
    > You are saying that the above assert is guaranteed never to fire? I'd
    > be surprised if that was true because un-initialized variables have
    > been a huge issue in just about every project I've ever worked on.


    Yes, but in this case the itsFoo variable is explicitly initialised (in
    the constructor of other_foo). The same is true with std::map, it
    creates a new object and initialises it with T(), where T is the type of
    the value in the key/value pair.

    --
    Erik Wikström
    Erik Wikström, May 31, 2008
    #7
  8. On 2008-06-01 01:14, Daniel T. wrote:
    > Erik Wikstré—£ wrote:
    >> Daniel T. wrote:
    >> > Victor Bazarov wrote:
    >> >
    >> > > Well, maybe he has no idea, maybe somebody else has no idea,
    >> > > but the Standard actuall says that the struct is
    >> > > value-initialised, which means that right before the assignment
    >> > > both values are 0. Therefore there is no need in a default
    >> > > c-tor.
    >> >
    >> > So given the below:
    >> >
    >> > struct foo {
    >> > int bar1;
    >> > };
    >> >
    >> > struct other_foo {
    >> > foo itsFoo;
    >> > other_foo():itsFoo() { }
    >> > };
    >> >
    >> > int main() {
    >> > other_foo obj;
    >> > assert(obj.itsFoo.bar1 == 0);
    >> > }
    >> >
    >> > You are saying that the above assert is guaranteed never to fire?
    >> > I'd be surprised if that was true because un-initialized
    >> > variables have been a huge issue in just about every project I've
    >> > ever worked on.

    >>
    >> Yes, but in this case the itsFoo variable is explicitly initialised
    >> (in the constructor of other_foo). The same is true with std::map,
    >> it creates a new object and initialises it with T(), where T is the
    >> type of the value in the key/value pair.

    >
    > I'm not sure how to take your "yes, but..." comment. Are you saying that
    > the assert will never fire, or are you saying that the above example is
    > somehow different?


    Sorry, I was a bit unclear. I meant it like this: Yes, uninitialised
    variables can cause a lot of trouble, but in this example the variable
    will be initialised.

    --
    Erik Wikström
    Erik Wikström, Jun 1, 2008
    #8
  9. Ron Eggler

    Guest

    On 30 May, 21:04, wrote:
    > On May 30, 1:16 pm, "Eric Pruneau" <> wrote:
    > > You could always use an online compiler like this onehttp://www.comeaucomputing.com/tryitout/

    >
    > I'm not aware of any others that are available online.  I would have
    > thought there would be some effort to move in that direction a long
    > time ago.  Comeau took a baby step but hasn't expanded on that in
    > years.   Just to give Comeau a little hint, adding https support
    > could be considered.  We're working on that and G-d willing it
    > will be available next month - June.


    See also http://www.dinkumware.com/exam/ for a selection of others.

    Cheers,
    Tony.
    , Jun 2, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    392
    RA Scheltema
    Jan 6, 2004
  2. Gunnar G

    struct in struct

    Gunnar G, May 31, 2004, in forum: C++
    Replies:
    14
    Views:
    797
  3. DanielEKFA
    Replies:
    8
    Views:
    601
    DanielEKFA
    May 16, 2005
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,370
    Chris Fogelklou
    Apr 20, 2004
  5. kl
    Replies:
    7
    Views:
    1,284
    James Kanze
    Jan 1, 2008
Loading...

Share This Page