map object in STL
-
I have a map of int, int. Someone told me that this statement below will create the element of index n1 and fill it with n2:
n2 = myMap[n1];
instead of the othe way around which works as well. THe benefit of the above method is that if that element already exists in the map, then in one shot we either accessed it (pree-exist) or created it (wasnt there). Seems so counterintuitive. Is it ccorrect? Didnt see anything like it in my books... Appreciate your help, ns
-
I have a map of int, int. Someone told me that this statement below will create the element of index n1 and fill it with n2:
n2 = myMap[n1];
instead of the othe way around which works as well. THe benefit of the above method is that if that element already exists in the map, then in one shot we either accessed it (pree-exist) or created it (wasnt there). Seems so counterintuitive. Is it ccorrect? Didnt see anything like it in my books... Appreciate your help, ns
myMap[n1] = n2; SHOULD work, ie create an entry if it does not already exit n2 = myMap[n1]; SHOULD NOT work, it should create an entry if it does not exist and give n2 its value.
-
I have a map of int, int. Someone told me that this statement below will create the element of index n1 and fill it with n2:
n2 = myMap[n1];
instead of the othe way around which works as well. THe benefit of the above method is that if that element already exists in the map, then in one shot we either accessed it (pree-exist) or created it (wasnt there). Seems so counterintuitive. Is it ccorrect? Didnt see anything like it in my books... Appreciate your help, ns
ns wrote: Someone told me that this statement below will create the element of index n1 and fill it with n2 it's not true, and you can prove it with the code below.
map<int, int> myMap;
int t = 50;
t = myMap[10];map<int, int>::iterator it;
for (it=myMap.begin(); it!=myMap.end(); it++)
{
printf("%d, %d\n", (*it).first, (*it).second);
}the output is 10, 0; not 10, 50. it will create the element, but the value part of it is the default value for the object (for an int, i guess the default is 0, in debug mode anyway). if you want to set the value, you have to use : myMap[10] = 50; -c
All you have to do is tell the people they are being attacked, and denounce the opposition for lack of patriotism and exposing the country to danger. -- Herman Goering, on how to control the public
-
myMap[n1] = n2; SHOULD work, ie create an entry if it does not already exit n2 = myMap[n1]; SHOULD NOT work, it should create an entry if it does not exist and give n2 its value.
-
ns wrote: Someone told me that this statement below will create the element of index n1 and fill it with n2 it's not true, and you can prove it with the code below.
map<int, int> myMap;
int t = 50;
t = myMap[10];map<int, int>::iterator it;
for (it=myMap.begin(); it!=myMap.end(); it++)
{
printf("%d, %d\n", (*it).first, (*it).second);
}the output is 10, 0; not 10, 50. it will create the element, but the value part of it is the default value for the object (for an int, i guess the default is 0, in debug mode anyway). if you want to set the value, you have to use : myMap[10] = 50; -c
All you have to do is tell the people they are being attacked, and denounce the opposition for lack of patriotism and exposing the country to danger. -- Herman Goering, on how to control the public