<i id='Ez4Tt'><tr id='Ez4Tt'><dt id='Ez4Tt'><q id='Ez4Tt'><span id='Ez4Tt'><b id='Ez4Tt'><form id='Ez4Tt'><ins id='Ez4Tt'></ins><ul id='Ez4Tt'></ul><sub id='Ez4Tt'></sub></form><legend id='Ez4Tt'></legend><bdo id='Ez4Tt'><pre id='Ez4Tt'><center id='Ez4Tt'></center></pre></bdo></b><th id='Ez4Tt'></th></span></q></dt></tr></i><div id='Ez4Tt'><tfoot id='Ez4Tt'></tfoot><dl id='Ez4Tt'><fieldset id='Ez4Tt'></fieldset></dl></div>
    <legend id='Ez4Tt'><style id='Ez4Tt'><dir id='Ez4Tt'><q id='Ez4Tt'></q></dir></style></legend>

      <bdo id='Ez4Tt'></bdo><ul id='Ez4Tt'></ul>

    <small id='Ez4Tt'></small><noframes id='Ez4Tt'>

    <tfoot id='Ez4Tt'></tfoot>

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

      时间:2024-05-12
          <tbody id='DC44f'></tbody>
          • <bdo id='DC44f'></bdo><ul id='DC44f'></ul>

            <i id='DC44f'><tr id='DC44f'><dt id='DC44f'><q id='DC44f'><span id='DC44f'><b id='DC44f'><form id='DC44f'><ins id='DC44f'></ins><ul id='DC44f'></ul><sub id='DC44f'></sub></form><legend id='DC44f'></legend><bdo id='DC44f'><pre id='DC44f'><center id='DC44f'></center></pre></bdo></b><th id='DC44f'></th></span></q></dt></tr></i><div id='DC44f'><tfoot id='DC44f'></tfoot><dl id='DC44f'><fieldset id='DC44f'></fieldset></dl></div>
            <legend id='DC44f'><style id='DC44f'><dir id='DC44f'><q id='DC44f'></q></dir></style></legend>

              <small id='DC44f'></small><noframes id='DC44f'>

                <tfoot id='DC44f'></tfoot>
                本文介绍了STL 映射是否在插入时初始化原始类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                map<wstring,int> Scores;
                

                它存储球员的名字和分数.当有人得分时,我会简单地做:

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

                Scores[wstrPlayerName]++;
                

                当地图中没有键为 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.

                如果我写的是:

                int i;
                i++;
                

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

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

                推荐答案

                operator[] 看起来像这样:

                operator[] looks like this:

                Value& map<Key, Value>::operator[](const Key& 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]++;
                

                此处的值是 int,并且整数默认构造为 0,就像您使用 int() 初始化它们一样.其他原始类型的初始化方式类似(egdouble()long()bool()等.).

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

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

                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.

                这篇关于STL 映射是否在插入时初始化原始类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么不将向量访问运算符指定为 noexcept? 下一篇:确定无序向量 T 是否存在拥有所有独特的元素

                相关文章

                • <bdo id='2lymL'></bdo><ul id='2lymL'></ul>
              1. <legend id='2lymL'><style id='2lymL'><dir id='2lymL'><q id='2lymL'></q></dir></style></legend>

                <small id='2lymL'></small><noframes id='2lymL'>

                  <i id='2lymL'><tr id='2lymL'><dt id='2lymL'><q id='2lymL'><span id='2lymL'><b id='2lymL'><form id='2lymL'><ins id='2lymL'></ins><ul id='2lymL'></ul><sub id='2lymL'></sub></form><legend id='2lymL'></legend><bdo id='2lymL'><pre id='2lymL'><center id='2lymL'></center></pre></bdo></b><th id='2lymL'></th></span></q></dt></tr></i><div id='2lymL'><tfoot id='2lymL'></tfoot><dl id='2lymL'><fieldset id='2lymL'></fieldset></dl></div>
                  <tfoot id='2lymL'></tfoot>