简体   繁体   English

C++:std::variant 可以保存向量、map 和其他容器吗?

[英]C++: Can std::variant hold vector, map, and other containers?

According to cppreference, variant is not allowed to allocate dynamic memory.根据 cppreference,不允许变体分配动态 memory。 This suggests that variant should not have dynamically-allocated containers as template, like vectors and maps.这表明变体不应该将动态分配的容器作为模板,如向量和地图。 Yet, some say that it is possible to have a vector as a variant template.然而,有人说可以将向量作为变体模板。 Could it be that the variant stores the vector pointer or reference, rather than the actual structure itself?难道变体存储了向量指针或引用,而不是实际的结构本身?

I would like to have a variant storing a vector and a map.我想要一个存储向量和 map 的变体。 I thought of 2 possibilities:我想到了两种可能:

std::variant<std::vector<int>, std::map<int, int> > x; //stores within the variant itself ??
std::variant <std::uniqur_ptr<std::vector<int> >, std::unique_ptr<std::map<int, int> > > y; //stores only a pointer. The container is allocated elsewhere.

I would prefer the first option for its simplicity.我更喜欢第一个选项,因为它很简单。 Let me know what you think!让我知道你的想法!

According to cppreference, variant is not allowed to allocate dynamic memory.根据 cppreference,不允许变体分配动态 memory。

You are misunderstanding what that means.你误解了这意味着什么。 std::variant is not allowed to be implemented by dynamically allocating the contained object, but that contained object is allowed to do whatever it normally does.不允许通过动态分配包含的 object 来实现std::variant ,但允许包含 object 的内容做它通常做的任何事情。

It is the difference between这是之间的区别

class incorrect_variant {
    union {
        std::vector<int> * vector;
        std::map<int, int> * map;
    } u;
    enum kind {
        is_vec,
        is_map,
    } k;
public:
    incorrect_variant(std::vector<int> value) : u(new std::vector<int>(value)), k(is_vec) {}

    // etc
}

class correct_variant {
    std::aligned_storage<std::max(sizeof(std::vector<int>), sizeof(std::map<int, int>)> storage;
    enum kind {
        is_vec,
        is_map,
    } k;
public:
    correct_variant(std::vector<int> value) : k(is_vec) 
    {
        new (storage) std::vector<int>(value);
    }

    // etc
}

std::vector<T> is a class with a pointer that manages dynamically allocated memory. std::vector<T>是一个 class,带有一个管理动态分配的 memory 的指针。

std::variant by not "being allowed to allocate memory" wouldn't be able to store the std::vector<T> object itself in dynamic memory, but the vector can manage its own dynamic memory just as fine. std::variant通过不“被允许分配内存”将无法将std::vector<T> object 本身存储在动态 memory 中,但向量可以管理自己的动态 ZCD69B4957F06CD818D7BF3D6198。

You're getting it wrong.你搞错了。

The variant class itself allocates not a single byte (otherwise it would probably have an allocator template argument). variant class 本身不分配一个字节(否则它可能会有一个分配器模板参数)。 Everything is locally in the class.一切都在 class 中。 But the "varianted" types themselves can allocate as much memory as they like.但是“变体”类型本身可以根据需要分配尽可能多的 memory。 They own their own memory and none of this concerns std::variant .他们拥有自己的 memory ,这与std::variant无关。 Isn't a bit like an array of std::string s.有点std::string的数组。 Sure, a C array doesn't allocate anything on its own, but the individual elements (strings) must do some allocation.当然,C 数组不会自行分配任何内容,但各个元素(字符串)必须进行一些分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C ++容器最小化内存开销(std :: map和std :: vector太贵) - Minimizing memory overhead using C++ containers (std::map and std::vector too expensive) C ++:向量容器和 <algorithm> std ::查找 - C++: vector containers and <algorithm> std::find C++ 中异构容器的 std::variant 与指向基类的指针 - std::variant vs pointer to base class for heterogeneous containers in C++ 具有std :: map的C ++向量? - C++ vector with std::map? 在 Visual Studio Code 中调试(C++ 程序)时看不到 vector 或其他容器(例如 map)的内容 - Can't see the contents of vector or other containers (e.g map) while debugging (C++ program) in Visual Studio Code 在C ++中,如何制作一个可以包含相同变体的矢量的变体? - In C++, how to make a variant that can contain a vector of of same variant? 从 std::variant 获取 substr <std::string, std::vector<std::string> &gt;&gt; 在 C++</std::string,> - Getting a substr from a std::variant<std::string, std::vector<std::string>>> in C++ 如何转换我的堆排序程序以使用 C++ 标准容器(例如 std::vector)? - How can I convert my heap sort program to use C++ standard containers (for example std::vector)? 使用std :: move将C ++复制映射到vector - C++ copy map to vector with std::move C++ 中的 std::variant cout - std::variant cout in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM