Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   struct in map (http://www.velocityreviews.com/forums/t617687-struct-in-map.html)

Ron Eggler 05-30-2008 05:47 PM

struct in map
 
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 ;)

Eric Pruneau 05-30-2008 07:16 PM

Re: struct in map
 

"Ron Eggler" <unknown@example.com> 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.



coal@mailvault.com 05-30-2008 08:04 PM

Re: struct in map
 
On May 30, 1:16*pm, "Eric Pruneau" <eric.prun...@cgocable.ca> wrote:
> "Ron Eggler" <unkn...@example.com> 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.

Ron Eggler 05-30-2008 09:08 PM

Re: struct in map
 
Eric Pruneau wrote:

>
> "Ron Eggler" <unknown@example.com> 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 ;)

Jim Langston 05-31-2008 12:55 AM

Re: struct in map
 
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
tazmaster@rocketmail.com



Daniel T. 05-31-2008 07:27 PM

Re: struct in map
 
"Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> Daniel T. wrote:
> > Ron Eggler <unkn...@example.com> 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.


Erik Wikström 05-31-2008 10:50 PM

Re: struct in map
 
On 2008-05-31 21:27, Daniel T. wrote:
> "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> Daniel T. wrote:
>> > Ron Eggler <unkn...@example.com> 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 06-01-2008 07:46 AM

Re: struct in map
 
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

guinness.tony@gmail.com 06-02-2008 01:06 PM

Re: struct in map
 
On 30 May, 21:04, c...@mailvault.com wrote:
> On May 30, 1:16*pm, "Eric Pruneau" <eric.prun...@cgocable.ca> 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.


All times are GMT. The time now is 04:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.