C++ (23)

1 Name: #!usr/bin/anon 2005-07-07 00:47 ID:/P+Qa0CG

I have this code:

typedef struct {
long a, b, c;
} Z;
map<string, Z> x;

Which I can use like this:

x["foo"].a = 0;
a = x["foo"].a;

The problem is what I really want to do is just have a fixed array of longs. I imagine accessing it like these:

x["foo"][i] = 0;
a = x["foo"][i];

So I tried this thing below, but it didn't work out.

map<string,long[3] > x;

What should I be doing??

2 Name: typical teenage game programmer 2005-07-07 02:42 ID:gAUUeEbh

two suggestions:

1st:

typedef struct {
long a[3];
long *operator[](int t){

    return &a[t];

}
} Z;
map<string, Z> x;

Which you can use like this:

long *mem=x["foo"][0];
*mem=0;
long a = *x["foo"][0];

2nd:

map<string, long > x[3];

which can be used like:

x[0]["foo"]=0;
long a = x[0]["foo"];

3 Name: dmpk2k!hinhT6kz2E 2005-07-07 02:52 ID:Heaven

map <string,long*> x;
x["foo"][0] = new long[10];

If you don't like the look of that, AFAIK the only other option in map<string,vector<long> > (note the space between the '> >')

4 Name: dmpk2k!hinhT6kz2E 2005-07-07 02:52 ID:Heaven

Sorry, "x["foo"] = new long[10];"

5 Name: typical teenage game programmer 2005-07-07 13:38 ID:4730s2aj

ahh yea i concidered tht way but i wasent sure if he wanted to allocate things

6 Name: typical teenage game programmer 2005-07-07 17:10 ID:4730s2aj

just curious in all but wat u making tht need string-ed arrays?

7 Name: #!usr/bin/anon 2005-07-07 23:08 ID:/P+Qa0CG

I need to use it to sort out a huge file that contains word-description. Depending on what's in the description, I basically need to set the 2nd level array.

Thank you both!

8 Name: typical teenage game programmer 2005-07-08 23:41 ID:FCG6MiYf

ahh tht minds me o sumthin i was gonna do. funny, still thinking bout doin it lol.

9 Name: #!/usr/bin/anonymous : 2007-09-27 05:34 ID:RE5U8oYX

Thread necromancy!

So, I heard that you shouldn't use the sizeof() function to calculate the size of a data type. Is this true or horse shit from the guy in my class that won't STFU?

10 Name: #!/usr/bin/anonymous : 2007-09-27 08:15 ID:Heaven

Horse shit.

What else would you use sizeof for?

11 Name: #!/usr/bin/anonymous : 2007-09-27 08:31 ID:RE5U8oYX

>>10

He said something about sizeof() being inaccurate about how it calculates size. I'm still trying to look for the facts though, but I've come up with nothing so far.

12 Name: #!/usr/bin/anonymous : 2007-09-27 08:38 ID:Heaven

>>11
Your source is full of shit. Sizeof does what it says on the tin; if the reader doesn't understand then it is his problem.

13 Name: #!/usr/bin/anonymous : 2007-09-27 12:53 ID:1+pbWKqX

Really, anything other than sizeof() is inaccurate! sizeof() exists exactly because there is no other way to know the size of a given datatype!

14 Name: #!/usr/bin/anonymous : 2007-09-27 17:09 ID:Heaven

>>13
there's also fixed size integers added with c99.

(u)intN_t

sizeof is a keyword, not a function. remove that () from there.
You use sizeof on operators*, objects and values.

*(cast) is considered an operator.

char *p = "string literal"
printf("%zu\n", sizeof p); /* prints sizeof(void*) */
char buf[] = "string literal"
printf("%zu\n", sizeof buf); /* prints sizeof "string literal" */

read http://4-ch.net/code/kareha.pl/1187046689/14-23 to learn more about similarities and differences between pointers and arrays.

15 Name: #!/usr/bin/anonymous : 2007-09-27 22:10 ID:RE5U8oYX

>>14

Thanks for that great thread. Can't understand why it was permasaged though...

16 Name: #!/usr/bin/anonymous : 2007-09-28 10:17 ID:Heaven

>>15
It started out supremely lame. The discussion developed because /code/ is a bit of a tumbleweed city at the best of times.

17 Name: #!/usr/bin/anonymous : 2007-09-29 12:51 ID:Heaven

>>15,16

> It started out supremely lame.

Hence why i gave a link to 14-23 posts only

18 Post deleted by moderator.

19 Name: #!/usr/bin/anonymous : 2007-09-30 12:21 ID:Heaven

>>18
cool!

20 Name: #!/usr/bin/anonymous : 2007-10-02 03:07 ID:Heaven

>>19
You didn't download that, did you?
If you did, perhaps you should consider a different hobby.

21 Name: #!/usr/bin/anonymous : 2007-10-02 15:57 ID:Heaven

>>20
You didn't really mean that, did you?
If you did, perhaps you should consider a different board.

22 Name: #!/usr/bin/anonymous : 2007-10-03 11:13 ID:Heaven

>>21
Oh no you didn't
Mmm-hmmmm

23 Name: #!/usr/bin/anonymous : 2007-10-06 16:33 ID:Heaven

gb2/gaia, gaiafags

Name: Link:
Leave these fields empty (spam trap):
More options...
Verification: