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?
To initialize the map with a random default value below is the approach: Approach: Declare a structure(say struct node) with a default value. Initialize Map with key mapped to struct node.
What exactly do you want to initialize to zero? map's default constructor creates empty map. You may increase map's size only by inserting elements (like m["str1"]=0 or m. insert(std::map<std::string,int>::value_type("str2",0)) ).
clear() function is used to remove all the elements from the map container and thus leaving it's size 0.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With