473,320 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

struct in map

Hi,

I would like to do something like this:

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

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 ;)
Jun 27 '08 #1
13 10197
Ron Eggler wrote:
Hi,

I would like to do something like this:

typedef struct{
unsigned short msg;
int SeqNr;
} MsgStruct;
Please consider abandoning this C-ism. In C++ we simply write

struct MsgStruct {
unsigned short msg;
int SeqNr;
};
map <string, MsgStructMyMsg;

Now I'm wondering how this is compatible with the standard and how I would
access the structs variables directly?
It is compatible. The requirements for the stored items are relatively
relaxed and such that PODs like your struct are supported. The items
need to be Assignable and Copyconstructible. PODs are.
Would it be something like this:
MyMsg["index"].msg= 0xabcd;?
That should work.
I'm not 100% sure if this would work and i don't have a compiler handy right
now to test this.
The indexing of your map returns a reference to the stored struct.
Using the member access operator on that reference is perfectly OK, and
changing the member via the assignment op is allowed too. No problem
AFAICS.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2

"Ron Eggler" <un*****@example.coma é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, MsgStructMyMsg;

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.
Jun 27 '08 #3
On May 30, 1:16*pm, "Eric Pruneau" <eric.prun...@cgocable.cawrote:
"Ron Eggler" <unkn...@example.coma é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, MsgStructMyMsg;
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.
Jun 27 '08 #4
Eric Pruneau wrote:
>
"Ron Eggler" <un*****@example.coma é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, MsgStructMyMsg;

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, MsgStructMyMsg;

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 ;)
Jun 27 '08 #5
Ron Eggler wrote:
Hi,

I would like to do something like this:

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

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
ta*******@rocketmail.com
Jun 27 '08 #6
Ron Eggler <un*****@example.comwrote:
I would like to do something like this:

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

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.

Something like this:

struct MsgStruct {
unsigned short msg;
int SeqNr;
MsgStruct(): msg(0), SeqNr(0) { }
};
Jun 27 '08 #7
Daniel T. wrote:
Ron Eggler <un*****@example.comwrote:
>I would like to do something like this:

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

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.
>
Something like this:

struct MsgStruct {
unsigned short msg;
int SeqNr;
MsgStruct(): msg(0), SeqNr(0) { }
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #8
"Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Daniel T. wrote:
Ron Eggler <unkn...@example.comwrote:
I would like to do something like this:
* * * * typedef struct{
* * * * * * * * unsigned short msg;
* * * * * * * * int SeqNr;
* * * * } MsgStruct;
* * * * map <string, MsgStructMyMsg;
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.

Jun 27 '08 #9
On 2008-05-31 21:27, Daniel T. wrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Daniel T. wrote:
Ron Eggler <unkn...@example.comwrote:
>I would like to do something like this:
> typedef struct{
unsigned short msg;
int SeqNr;
} MsgStruct;
map <string, MsgStructMyMsg;
>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
Jun 27 '08 #10
Erik Wikström 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?
Jun 27 '08 #11
Daniel T. wrote:
Erik Wikström 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?
It's not different, the 'foo' is, in fact, value-initialised, so
the 'bar1' member should be value-initialised, that is, for built-
in types, zero-initialised. If you see that it isn't, i.e. the
assertion fails, then ditch the compiler you use, and move on to
a standard-compliant one.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #12
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
Jun 27 '08 #13
On 30 May, 21:04, c...@mailvault.com wrote:
On May 30, 1:16*pm, "Eric Pruneau" <eric.prun...@cgocable.cawrote:
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 27 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.