C ++将多个类型推送到向量 [英] C++ Push Multiple Types onto Vector

查看:192
本文介绍了C ++将多个类型推送到向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:我知道类似的问题在此之前已被问过,但我没有发现它们有帮助或非常清楚。

Note: I know similar questions to this have been asked on SO before, but I did not find them helpful or very clear.

第二个备注:对于此项目/作业的范围,我尝试避免使用第三方库,例如Boost。

Second note: For the scope of this project/assignment, I'm trying to avoid third party libraries, such as Boost.

我我试图看看是否有一种方式,我可以有一个单一的向量持有多个类型,在其每个索引。例如,假设我有以下代码示例:

I am trying to see if there is a way I can have a single vector hold multiple types, in each of its indices. For example, say I have the following code sample:

vector<something magical to hold various types> vec;
int x = 3;
string hi = "Hello World";
MyStruct s = {3, "Hi", 4.01};

vec.push_back(x);
vec.push_back(hi);
vec.push_back(s);

我听说过矢量< void *> 可以工作,但是然后它变得棘手与内存分配,然后总是存在的可能性,附近的存储器中的某些部分可能会无意中覆盖,如果插入某个索引的值大于预期。

I've heard vector<void*> could work, but then it gets tricky with memory allocation and then there is always the possibility that certain portions in nearby memory could be unintentionally overridden if a value inserted into a certain index is larger than expected.

在我的实际应用中,我知道什么可能的类型可以插入到向量中,但是这些类型并不都来自同一个超类,并且没有保证所有这些类型将被推到向量或以什么顺序。

In my actual application, I know what possible types may be inserted into a vector, but these types do not all derive from the same super class, and there is no guarantee that all of these types will be pushed onto the vector or in what order.

有一种方法,我可以安全完成目标

Is there a way that I can safely accomplish the objective I demonstrated in my code sample?

推荐答案

p>为了这样做,你肯定需要一个包装类来以某种方式隐藏你的对象从向量的类型信息。

In order to do that, you'll definitely need a wrapper class to somehow conceal the type information of your objects from the vector.

这可能也很好这个类在您以前存储了Type-B时尝试获取Type-A时抛出异常。

It's probably also good to have this class throw an exception when you try to get Type-A back when you have previously stored a Type-B into it.

这里是Holder类的一部分的我的项目。您可以从这里开始。

Here is part of the Holder class from one of my projects. You can probably start from here.

注意:由于使用不受限制的联合,这只能在C ++ 11中使用。关于这一点可以在这里找到: C ++ 0x中提出的无限制联盟是什么?

Note: due to the use of unrestricted unions, this only works in C++11. More information about this can be found here: What are Unrestricted Unions proposed in C++0x?

class Holder {
public:
    enum Type {
        BOOL,
        INT,
        STRING,
        // Other types you want to store into vector.
    };

    template<typename T>
    Holder (Type type, T val);

    ~Holder () {
        // You want to properly destroy
        // union members below that have non-trivial constructors
    }

    operator bool () const {
        if (type_ != BOOL) {
           throw SomeException();
        }
        return impl_.bool_;
    }
    // Do the same for other operators
    // Or maybe use templates?

private:
    union Impl {
        bool   bool_;
        int    int_;
        string string_;

        Impl() { new(&string_) string; }
    } impl_;

    Type type_;

    // Other stuff.
};

这篇关于C ++将多个类型推送到向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆