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

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

      <tfoot id='u01Ti'></tfoot>

      1. 是否可以?std::vector<double>my_vec(sz);已分配但未初始化或填充

        时间:2023-06-05

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

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

                • <legend id='bqc2B'><style id='bqc2B'><dir id='bqc2B'><q id='bqc2B'></q></dir></style></legend>
                    <tbody id='bqc2B'></tbody>

                  <tfoot id='bqc2B'></tfoot>
                • 本文介绍了是否可以?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);已分配但未初始化或填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文章

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

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