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

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

      2. 是否可以?std::vector<double>my_vec(sz);已分配但未

        时间:2023-10-07

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

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

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

            <tbody id='EyRVJ'></tbody>

                  <bdo id='EyRVJ'></bdo><ul id='EyRVJ'></ul>
                • 本文介绍了是否可以?std::vector<double>my_vec(sz);已分配但未初始化或填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 [C++11 中的值初始化对象和std::vector 构造函数,Channel72 询问,

                  At [Value-Initialized Objects in C++11 and std::vector constructor, Channel72 asks,

                  问题:我的理解正确吗?如果 T 是 POD,显式 std::vector(size_type count) 是否提供未初始化的数组(类似于 malloc)?

                  答案是否定的.

                  我的问题是,好吧,那是什么?"

                  My question is, "Okay then, what does?"

                  Nevin 的其中一个回答暗示要回答我的问题.澄清一下,我的问题是,有没有办法使用 std::vector 而不用零或其他什么东西来填充分配的内存?

                  One of the responses, by Nevin, hints at answering my question. To clarify, my question is, Is there a way to use std::vector<double> without it gratuitously filling allocated memory with zeros or whatever?

                  我不是在寻求解决方法,例如以零大小启动向量并使用 push_back().这并不总是可能的,此外,在这一点上,我只想弄清楚它,而不是因为我想弄清楚它.

                  I am not asking for workarounds, like starting the vector at zero size and using push_back(). That is not always possible, and besides, at this point I want to get it figured out for no other reason than I want to get it figured out.

                  我无法获得 Nevin 的建议(自定义分配器)进行编译.VC++ 2017rc (Dinkum) 以其通常难以理解的方式抱怨.关于 std::_Wrap_alloc 的一些东西.Nevin 的代码不完整,我可能不知道如何完成它.在我看到他之前,我编写了自己的自定义分配器,它似乎有效,但我对自己的理解不够自信,无法发誓.

                  I cannot get Nevin's suggestion, a custom allocator, to compile. VC++ 2017rc (Dinkum) complains in its usual inscrutable way. Something about std::_Wrap_alloc. Nevin's code is incomplete, and I probably do not know how to complete it. Before I saw his, I wrote my own custom allocator which seems to work, but I am not confident in my understanding enough to swear by it.

                  在我对此感到困惑的时间里,我本可以写一个不那么教条的替代 std::vector,加上美国伟大小说的几章.

                  For the time I have spent puzzling over this, I could have written a less dogmatic replacement for std::vector, plus several chapters of the Great American Novel.

                  推荐答案

                  万岁!理查德·克里顿(Richard Critten)来救援!他在问题下的评论直接指向答案.

                  HOORAY! Richard Critten to the rescue! His comment under the question leads directly to the answer.

                  零喷射的罪魁祸首是默认的分配器模板,即 std::allocator.所以我们替换它,或者用一个分配器适配器修改它.

                  The zero-spewing culprit is the default allocator template, namely std::allocator. So we replace it, or modify it with an allocator adapter.

                  我稍微整理了一下代码,并扩展了注释.比尔,请随时发布更全面的答案.但是下面的方法非常好.

                  I tidied up the code a little, and expanded the comments. Bill, please feel free to post a more comprehensive answer. But the following does the trick very nicely.

                  // Allocator adapter
                  // Given an allocator A, (std::allocator by default), this adapter 
                  // will, when feasible, override A::construct() with a version that 
                  // employs default construction rather than value-initialization.
                  // "Feasible" means the object (U *ptr) is default-constructable and
                  // the default constructor cannot throw exceptions.
                  // 
                  // Thus it thwarts gratuitous initializations to zeros or whatever.
                  
                  template <typename T, typename A = std::allocator<T>>
                  class default_init_allocator : public A {
                      typedef std::allocator_traits<A> a_t;
                  public:
                      // http://en.cppreference.com/w/cpp/language/using_declaration
                      using A::A; // Inherit constructors from A
                  
                      template <typename U> struct rebind {
                          using other =
                              default_init_allocator
                              <  U, typename a_t::template rebind_alloc<U>  >;
                      };
                  
                      template <typename U>
                      void construct(U* ptr)
                          noexcept(std::is_nothrow_default_constructible<U>::value) {
                          ::new(static_cast<void*>(ptr)) U;
                      }
                  
                      template <typename U, typename...Args>
                      void construct(U* ptr, Args&&... args) {
                          a_t::construct(static_cast<A&>(*this),
                              ptr, std::forward<Args>(args)...);
                      }
                  };
                  

                  这篇关于是否可以?std::vector<double>my_vec(sz);已分配但未初始化或填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:缩小向量 下一篇:如何存储向量&lt;bool&gt;或一个位集到文件中

                  相关文章

                  最新文章

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

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

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

                • <tfoot id='qksvX'></tfoot>