C++ map<int,int> 默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2667355/
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
map<int,int> default values
提问by Bill Kotsias
std::map<int,int> mapy;
++mapy[5];
Is it safe to assume that mapy[5]
will always be 1? I mean, will mapy[5]
always get the default value of 0 before '++', even if not explicitly declared, as in my code?
假设它mapy[5]
总是 1是否安全?我的意思是,mapy[5]
即使没有像我的代码那样明确声明,在 '++' 之前总是会得到默认值 0?
回答by rep_movsd
As soon as you access the map with the [] operator, if the key doesn't exist it gets added. The default initializer of the int type gets invoked - so it will get a value of 0.
只要您使用 [] 运算符访问地图,如果键不存在,它就会被添加。int 类型的默认初始值设定项被调用 - 因此它将获得值 0。
回答by Lightness Races in Orbit
Yes, it is safe to assume.
是的,可以安全地假设。
The map's operator[]
is specified thus:([map.access])
地图operator[]
是这样指定的:([map.access])
Effects:If there is no key equivalent to
x
in the map, insertsvalue_type(std::move(x), T())
into the map.
Returns:A reference to themapped_type
corresponding tox
in*this
.
效果:如果
x
映射中没有等效键,则插入value_type(std::move(x), T())
到映射中。
返回:对mapped_type
对应于x
in 的引用*this
。
T()
uses value-initialisationfor all T
except void
([expr.type.conv]/2), and value-initialisationfor a primitive results in zero-initialization([dcl.init]/7).
T()
除了([expr.type.conv]/2)之外的所有使用值初始化,并且原始值的值初始化导致零初始化([dcl.init]/7)。T
void
Therefore, the expression evaluates to a reference to an object with value zero ([dcl.init]/5).
因此,该表达式的计算结果是对值为 0 ([dcl.init]/5)的对象的引用。
The operator++
call then increments that object to one, and evaluates to one.
然后operator++
调用将该对象增加到 1,并计算结果为 1。
(All references are C++11.)
(所有参考文献均为 C++11。)
回答by Rasmus Kaj
Yes, the default value will be the default of that type. If you want another default, you can create a class that behaves like an int but has a different default constructor.
是的,默认值将是该类型的默认值。如果您想要另一个默认值,您可以创建一个行为类似于 int 但具有不同默认构造函数的类。
回答by Rachel Casey
Rep_Movsd's answer is oversimplified and is likely to lead to numerous extremely dangerous misconceptions. Primitive data-types in C++ do not have initializers. Louis Brandy had a wonderful talk in which he discussed many common C++ errors made at Facebook and a misunderstanding of how std::map<>[] works was one of the errors that he discussed, this is an excellent resource although he doesn't go into detail as to how std::map<>[] actually works.
Rep_Movsd 的回答过于简单化,很可能会导致许多极其危险的误解。C++ 中的原始数据类型没有初始值设定项。Louis Brandy 有一个精彩的演讲,他讨论了在 Facebook 上犯的许多常见的 C++ 错误,对 std::map<>[] 工作原理的误解是他讨论的错误之一,这是一个很好的资源,尽管他没有详细了解 std::map<>[] 的实际工作方式。
In general, ints are not initialized and are undefined like all primitive types. That being said when used with std::map<>[] the int has a default value of zero set through a process called value initialization. Value initialization is a process that actually works with structs in general. For instance,
通常,int 不会像所有原始类型一样被初始化和未定义。话虽如此,当与 std::map<>[] 一起使用时,int 的默认值为零,通过称为值初始化的过程设置。值初始化是一个实际上与一般结构一起工作的过程。例如,
struct Struct {
Struct() : memberVariable() {}
int memberVariable;
};
Will always initialize the int to zero. If the member variables were other primitive types they would also have specific initialization values. For instance, the following types are initialized, through value initialization like so:
bool = false
float = 0.0f
enum = (enum type)0
pointer = null pointer
pointer to member = null member pointer
将始终将 int 初始化为零。如果成员变量是其他原始类型,它们也将具有特定的初始化值。例如,以下类型被初始化,通过像这样的值初始化:
bool = false
float = 0.0f
enum = (enum type)0
pointer = null pointer
pointer to member = null member pointer
Be extremely careful when working with data that is not explicitly initialized. One last thing, consider the following code
处理未显式初始化的数据时要格外小心。最后一件事,请考虑以下代码
map<string, int> myMap;
cout << myMap["Foo"];
This code not only will always initialize the integer to 0, but it will also insert 0 into the map. Just to quickly recap, primitive data types are undefined if not initialized, but in some instances such as with a struct or map value initialization will initialize the primitive data with a specific value.
这段代码不仅总是将整数初始化为 0,而且还会将 0 插入到映射中。快速回顾一下,原始数据类型如果未初始化则是未定义的,但在某些情况下,例如使用结构或映射值初始化将使用特定值初始化原始数据。