如何在 C++ 中设置大小并将映射初始化为零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6234891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the size and initialize a map to zero in c++
提问by Shadi
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?
我正在声明一个地图,例如map<string,int> registers
. 如何将其设置为特定大小以及如何将其所有值设置为零,以便稍后在映射值中插入值?
回答by Martin York
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).
这是有效的,因为即使registers
是空的。运算符 [] 将键插入映射并将其值定义为类型的默认值(在这种情况下整数为零)。
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
回答by Chris A.
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:
这可能比语法/“如何”问题更像是一个概念问题。是的,正如许多海报/评论者所说的那样,仅通过使用给定密钥进行初始访问,就会创建相应的值并将其默认设置为 0。但是您使用的一个不合适的重要短语是:
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?
一行摘要:您不想自己初始化值而不是让 map 为任何给定键的值分配其默认值吗?
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;
}