PDA

View Full Version : map question

mickey
6th June 2006, 17:25
Hi, it happen to me this thing: I create lightmap[4] element and dimensione map became 4; is I do lightmap[6] = Light(); then 6 element...why this?

map<int,Light> lightmap;
lightmap[4] = Light();

michael
6th June 2006, 17:33
Hi, it happen to me this thing: I create lightmap[4] element and dimensione map became 4; is I do lightmap[6] = Light(); then 6 element...why this?

map<int,Light> lightmap;
lightmap[4] = Light();

I don't understand what you are asking.

mickey
6th June 2006, 17:36
lightmap[4] = light();
int i = lightmap.size();
printf("ADDING LIGHT dimensione <map> %i\n" ,i); //this print 4...

I thought lightmap[4] = light(); create an element with index <int=4, > ...

michael
6th June 2006, 19:11
lightmap[4] = light();
int i = lightmap.size();
printf("ADDING LIGHT dimensione <map> %i\n" ,i); //this print 4...

I thought lightmap[4] = light(); create an element with index <int=4, > ...

I am still not completely understanding you. If you mean that it is creating them element lightmap[4] then yes it does do that. If you are mean that it should create elements lightmap[0] through lightmap[4] then no it does not do that.

lightmap.size() only returns how many elements the map has, not the key of any one particular element. So, if you did the following:

map<int, Light> lightmap;
lightmap[3] = light();
lightmap[4] = light();
lightmap[5] = light();
int i = lightmap.size();
printf("ADDING LIGHT dimensione <map> %i\n" ,i);

The output would be 3.

A map is declared as "map <key type, value type, ...>" where the key type is the type of key you will be using. You can then use the brackets [] to get or set the value at map element [key]. So when you call lightmap[4] you are NOT telling it to create lightmap[0] through lightmap[4], you are simply saying I am holding this value at element location named 4 in the map named lightmap. You could have declared your map as:
map<string, Light> lightmap;

And changed
lightmap[4] = light();
to
lightmap["Four"] = light();

Does this help?

bood
7th June 2006, 03:31
lightmap[4] = light();
int i = lightmap.size();
printf("ADDING LIGHT dimensione <map> %i\n" ,i); //this print 4...

I thought lightmap[4] = light(); create an element with index <int=4, > ...

The `i` should be 1, and I cann't reproduce your problem...
here's my code

class A
{
};
int main()
{
map<int,A> m;
m[4]=A();
cout<<m.size();
return 0;
}

and it prints `1`

mickey
7th June 2006, 08:09
my problem probabily was another:

map<int, Light> lightmap;
lightmap[3] = light();
lightmap[4] = light();.
lightmap[5] = light();
int i = lightmap.size();
printf("ADDING LIGHT dimensione <map> %i\n" ,i); "ADDING LIGHT dimensione <map> %i\n" ,i); //this should print 3
now I need to to a thing (but I dont know how):

int size = lightmap.size();
for (int i=0; i<size; i++) {
lightmap[i].DOSomething();
}
here above I need to put in a for a my Light class function; but the for is working on elements 0,1,2; I need to use map with KEY 3,4,5 (they're iserted before).
Are u understand? thanks

wysota
7th June 2006, 08:27
for(QMap<int, Light>::iterator iter = lightmap.begin(); iter!=lightmap.end();++iter){
iter.data().doSomething();
}

The same goes with std::map, just the iterator may behave different (just look at its docs to see what member you need to access). It'll probably be "iter->doSomething()".

bood
8th June 2006, 05:11
wysota's right, just use the`iterator`

And seems your English's poor:p

Michiel
10th June 2006, 09:04
And seems your English's poor:p

And it seems your English's poor. ;)

bood
10th June 2006, 12:55
And it seems your English's poor. ;)

I thought it can be ignored in oral English...can't it?

mickey
10th June 2006, 15:30
Are we speaking?

wysota
10th June 2006, 17:30
Stop the flame, please.

Michiel
10th June 2006, 18:40
I thought it can be ignored in oral English...can't it?

Not if you prepend it with 'And'. Anyway, what relevance does it have? As long as it's not 1337-speak and we can understand each other...