Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the size and initialize a map to zero in c++

Tags:

c++

maps

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?

like image 980
Shadi Avatar asked Jun 04 '11 05:06

Shadi


People also ask

How do you initialize a map 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.

How does one declare and initialize a map of size 0?

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)) ).

How do you initialize a map to 0?

clear() function is used to remove all the elements from the map container and thus leaving it's size 0.


2 Answers

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
like image 125
Martin York Avatar answered Sep 20 '22 09:09

Martin York


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;
    }
like image 38
Chris A. Avatar answered Sep 19 '22 09:09

Chris A.