struct in map

R

Ron Eggler

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
 
E

Eric Pruneau

Ron Eggler said:
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.
 
C

coal

"Ron Eggler" <[email protected]> a écrit dans le message de K4X%j.3$Gn.1@edtnps92...




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

Ron Eggler

Eric said:
Ron Eggler said:
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! :)
 
J

Jim Langston

Ron said:
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;
};
 
D

Daniel T.

Victor Bazarov said:
Daniel said:
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.
 
E

Erik Wikström

Victor Bazarov said:
Daniel said:
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.
 
E

Erik Wikström

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

guinness.tony

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top