STL 映射在插入时初始化原始类型吗?

发布于 2024-07-12 08:33:19 字数 432 浏览 6 评论 0原文

我有一个像这样的 std::map :

map<wstring,int> Scores;

它存储玩家的姓名和分数。 当有人获得分数时,我会简单地执行以下操作:

Scores[wstrPlayerName]++;

当地图中没有带有键 wstrPlayerName 的元素时,它将创建一个,但它在增量之前初始化为零或 null 还是未定义?

我应该在每次增量之前测试该元素是否存在吗?

我只是想知道,因为我认为原始类型的东西在创建时总是未定义的。

如果我写这样的内容:

int i;
i++;

编译器警告我 i 未定义,当我运行程序时它通常不为零。

I have a std::map like this:

map<wstring,int> Scores;

It stores names of players and scores. When someone gets a score I would simply do:

Scores[wstrPlayerName]++;

When there is no element in the map with the key wstrPlayerName it will create one, but does it initialize to zero or null before the increment or is it left undefined?

Should I test if the element exists every time before increment?

I just wondered because I thought primitive-type things are always undefined when created.

If I write something like:

int i;
i++;

The compiler warns me that i is undefined and when I run the program it is usually not zero.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自此以后,行同陌路 2024-07-19 08:33:19

operator[] 看起来像这样:

Value& map<Key, Value>::operator[](const Key& key);

如果您使用映射中尚未存在的 key 调用它,它将默认构造一个 Value 的新实例,并将其放入映射到您传入的 key 下,并返回对其的引用。 在本例中,您得到:

map<wstring,int> Scores;
Scores[wstrPlayerName]++;

这里的值是 int,并且 int 默认构造为 0,就像您使用 int() 初始化它们一样。 其他基本类型的初始化类似(例如double()long()bool()等.)。

最后,您的代码在映射中放置一个新对 (wstrPlayerName, 0),然后返回对 int 的引用,然后将其递增。 因此,如果您希望元素从 0 开始,则无需测试该元素是否存在。

operator[] looks like this:

Value& map<Key, Value>::operator[](const Key& key);

If you call it with a key that's not yet in the map, it will default-construct a new instance of Value, put it in the map under key you passed in, and return a reference to it. In this case, you've got:

map<wstring,int> Scores;
Scores[wstrPlayerName]++;

Value here is int, and ints are default-constructed as 0, as if you initialized them with int(). Other primitive types are initialized similarly (e.g., double(), long(), bool(), etc.).

In the end, your code puts a new pair (wstrPlayerName, 0) in the map, then returns a reference to the int, which you then increment. So, there's no need to test if the element exists yet if you want things to start from 0.

窗影残 2024-07-19 08:33:19

这将默认构造一个 value 的新实例。 对于整数,默认构造是 0,因此这可以按预期工作。

This will default-construct a new instance of value. For integers, the default construction is 0, so this works as intended.

深白境迁sunset 2024-07-19 08:33:19

在增加该项目之前,不应测试该项目是否存在。 正如其他人所说, [] 运算符完全按照您需要的方式执行操作。

但是如果默认构造的值对您不起作用怎么办? 在您的情况下,查找元素是否已存在的最佳方法是尝试插入它。 std::mapinsert 成员函数返回一个 std::pair。 无论插入成功还是失败,该对的第一个元素都将指向所需的对象(或者是新的对象,或者是已经存在的对象)。 然后您可以根据需要更改其值。

You should not test if the item exists before incrementing it. The [] operator does exactly what you need it to do, as others have said.

But what if the default-constructed value wouldn't work for you? In your case the best way to find if the element already exists is to try to insert it. The insert member function for std::map returns a std::pair<iterator, bool>. Whether the insert succeeds or fails, the first element of the pair will point to the desired object (either your new one, or the one that was already present). You can then alter its value as you see fit.

冰火雁神 2024-07-19 08:33:19

检查初始化规则。

请参见 C++ Prog Lang 或 C++ std 书的第 4.9.5 节初始化。 根据您的变量是本地变量、静态变量、用户定义变量还是常量变量,可能会发生默认初始化。

在你的例子中,int被称为POD(普通旧数据类型)。 任何自动(在堆/局部变量上创建)POD 变量都不会默认初始化。 因此,对于您来说,上面的“i”的值不会为零。

始终养成在堆中定义时初始化 POD 的习惯。 您甚至可以使用 int() 来初始化值。

Check rules for initialization.

See section 4.9.5 Initialization of C++ Prog Lang or C++ std book. Depending on whether your variable is local, static, user-defined or const default initialization can happen.

In you case, int is called POD (Plain old Datatype). Any auto (created on heap / local variable) POD variable is not default initialized. Hence for you "i" above will not have value zero.

Always make an habit of initializing POD when defined in heap. You can even use int() to initialize value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文