I am declaring a map such as map<string,int> registers
. How can I set it to a specific size and how can I set all its values to zeros, so that I can later insert values in the mapped value?
How about this:
std::map<std::string, int> registers; // done.
// All keys will return a value of int() which is 0.
std::cout << registers["Plop"] << std::endl; // prints 0.
This works because even though registers
is empty. The operator [] will insert the key into the map and define its value as the default for the type (in this case integers are zero).
So the sub-expression:
registers["Plop"];
Is equivalent to:
if (registers.find("Plop") == registers.end())
{
registers.insert(make_pair("Plop", 0));
}
return registers.find("Plop").second; // return the value to be used in the expression.
This also means the following works fine (even if you have not defined the key before).
registers["AnotherKey"]++; // Increment the value for "AnotherKey"
// If this value was not previously inserted it will
// first be inserted with the value 0. Then it will
// be incremented by the operator ++
std::cout << registers["AnotherKey"] << std::end; // prints 1
how can i set it to a specific size and how can i set all its values to zeros
This is probably much more of a conceptual problem than a syntax/"how to" problem. Yes, it's true that just by initial access with a given key, that corresponding value is going to be created and set to 0 by default as many posters/commenters said. But an important phrase that you used that doesn't fit is:
all its values
It has no values to begin with and if you're programming with the assumption that it does, this alone can be cause for conceptual errors.
It's typically better to be in the habit of finding whether the key is in your map and then, if not, do something to initialize it. Even though this might seem like just overhead, doing things explicitly like this will likely prevent conceptual errors in the future, specifically when you try to access a key's value without knowing whether it was already in the map.
One line summary: don't you want to initialize the value yourself rather than having map assign its default value for any given key's value?
Thus the code you probably want to use is:
if(mymap.find(someKey) == mymap.end())
{
// initialize it explicitly
mymap[someKey] = someInitialValue;
}
else
{
// it has a value, now you can use it, increment it, whatever
mymap[someKey] += someIncrement;
}
In map every key is mapped with default value zero when the map is declared. But if you want to initialize with some value you can follow this approach. For Structure,
struct Node {
int value = 1;
}
For Map with every key mapped to default value 1.
Map < int, Node > M;
© 2022 - 2024 — McMap. All rights reserved.
m["abc"] = 1;
? – Plainsmanm["str1"]=0
orm.insert(std::map<std::string,int>::value_type("str2",0))
). – Stefanysteffanefor
loop if you want. :) What are you trying to do anyway? – Stefanysteffanemap["key"]
will return 0 (and insert "key" into the map) if "key" is not in the map -- so, the values will all be zero when you access them the first time – Zibetm[key]
it will be zero. You don't have to do anything! – Ybarra