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??
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"];
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 '> >')
Sorry, "x["foo"] = new long[10];"
ahh yea i concidered tht way but i wasent sure if he wanted to allocate things
just curious in all but wat u making tht need string-ed arrays?
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!
ahh tht minds me o sumthin i was gonna do. funny, still thinking bout doin it lol.
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?
Horse shit.
What else would you use sizeof for?
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.
>>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.
Really, anything other than sizeof() is inaccurate! sizeof() exists exactly because there is no other way to know the size of a given datatype!
>>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.
Thanks for that great thread. Can't understand why it was permasaged though...
>>15
It started out supremely lame. The discussion developed because /code/ is a bit of a tumbleweed city at the best of times.
>>18
cool!
>>21
Oh no you didn't
Mmm-hmmmm
gb2/gaia, gaiafags