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

Is it possible? std::vectorlt;doublegt; my_vec(sz); which is allocated but not initialized or filled(是否可以?std::vectordoublemy_vec(sz);已分配但未初始化或填充)

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

本文标题为:是否可以?std::vector<double>my_vec(sz);已分配但未初始化或填充

基础教程推荐