<legend id='tSoab'><style id='tSoab'><dir id='tSoab'><q id='tSoab'></q></dir></style></legend>

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

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

        为什么 std::vector 的元素不需要默认构造函数?

        时间:2023-09-15

          <small id='6eXQ6'></small><noframes id='6eXQ6'>

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

            • <bdo id='6eXQ6'></bdo><ul id='6eXQ6'></ul>

            • <tfoot id='6eXQ6'></tfoot><legend id='6eXQ6'><style id='6eXQ6'><dir id='6eXQ6'><q id='6eXQ6'></q></dir></style></legend>

                  本文介绍了为什么 std::vector 的元素不需要默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我如何编写自己的数组类而不需要它的元素的默认构造函数?现在,当我使用 new [] 分配空间时,我需要一个默认构造函数.

                  And how can I write my own array class to not need a default constructor for its elements? Right now, when I do the new [] to allocate space, I need a default constructor.

                  std::vector 没有.

                  std::vector does not.

                  他们是如何施展这种魔法的?

                  How do they do this magic?

                  推荐答案

                  std::vector 不需要默认构造函数,因为它从不使用它.每次它需要构造一个元素时,它都会使用复制构造函数来完成,因为每次它都有要复制的东西:现有的向量元素或您自己提供的用于通过方法参数复制的元素(显式或隐式,依赖于默认参数)

                  std::vector doesn't need the default constructor because it never uses it. Every time it needs to construct an element, it does it by using the copy constructor, because every time it has something to copy: either existing vector element or an element you yourself supplied for copying through a method's parameter (explicitly or implicitly, by relying on a default argument)

                  您可以以完全相同的方式编写这样的类:每次您需要在数组中构造一个新元素时,都需要用户提供一个元素进行复制.在这种情况下,构建原始元素成为用户的责任.

                  You can write a class like that in exactly the same way: every time you need to construct a new element in your array, require the user to supply an element for copying. In this case constructing that original element becomes user's responsibility.

                  每次看起来好像std::vector需要"你的默认构造函数时,它只是意味着你在某处依赖了某些vector的默认参数code>s 方法,即是 you 试图默认构造一个元素,而不是向量.同样,向量本身永远不会尝试默认构造元素.

                  Every time it appears as if std::vector "requires" a default constructor from you, it simply means that somewhere you relied on a default argument of some of the vectors methods, i.e. it was you who tried to default-construct an element, not the vector. The vector itself, again, will never try to default-construct elements.

                  为了避免内存分配期间的默认构造函数要求,标准库分配原始未初始化内存块,然后立即在该原始内存块中复制构造新元素(这是new[] 做不到).此功能封装在 std::allocator 类中.您也可以在代码中使用 std::allocator,这意味着您也可以立即使用魔法".

                  In order to avoid the default constructor requirement during memory allocation, standard library allocates raw uninitialized memory block and then immediately copy-constructs new elements in that raw memory block (which is something new[] cannot do). This functionality is incapsulated in std::allocator class. You can use std::allocator in your code as well, meaning that the "magic" is immediately available to you too.

                  注意: 以上适用于 C++11 之前版本的 C++ 语言规范.C++11 改变了很多东西.并且这些更改确实创建了 std::vector 可以在内部使用默认构造函数的情况.

                  Note: The above applies to pre-C++11 version of C++ language specification. C++11 changed a lot of things. And these changes do create situations in which std::vector can use default constructors internally.

                  另外值得注意的是,即使是原始的 C++98 规范也允许实现使用函数重载而不是默认参数来实现标准库接口.这意味着在形式上可以有一个有效的 std::vector 的 C++98 实现,它在内部使用默认构造函数.

                  Also it might be worth noting that even the original C++98 specification allowed implementations to use function overloading instead of default arguments in order to implement the standard library interface. This means that formally it is possible to have a valid C++98 implementation of std::vector that uses default constructors internally.

                  这篇关于为什么 std::vector 的元素不需要默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在C++中将文件读入向量? 下一篇:通过迭代器与通过运算符 []/索引访问 std::vector

                  相关文章

                  最新文章

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

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

                1. <tfoot id='pmUaD'></tfoot>