如何在C ++中获得不同向量的向量 [英] How to get a vector of different vectors in C++

查看:59
本文介绍了如何在C ++中获得不同向量的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个表的C ++表示形式,如下所示:

I'd like to have a C++ representation of a table like the following:

0   1   2
=   =   =
1  1.0  a
2  2.0  b
3  3.0  c

在运行时,必须从 int double string 中选择列的类型.

The types of the columns have to be chosen from int, double or string at runtime.

用C ++表示的最好方法是什么?

What is the best way to express it in C++?

附录:我的真正问题:我想要一个数据库表的列表示形式,该表可以具有任意SQL类型(我将使用 int double string ).

Addendum: my real problem: I want a columnar representation of a database table which can have arbitrary SQL types (I'll settle for int, double and string).

推荐答案

在运行时创建动态类型的对象,即Google工厂模式".

edit: to create an object of dynamic type at runtime, google "factory pattern".

您需要第一个向量包含一致的类型,因此(在C ++中)您可能想要一个基类并从中派生.我不确定std :: vector类是否具有未类型化的基(可能).

You need the first vector to contain a consistent type, so you probably (in C++) want to have a base class and derive from it. I'm not sure if the std::vector class has an untyped base (it might).

因此,您可以围绕向量的概念创建包装类(yuck),或者创建变体类型的向量(yuck),或者创建某种变体向量(yuck).我可能会选择后者,因为它是最简单的方法,其中仍然包含类型安全的向量.

So, you either create a wrapper class around the concept of a vector (yuck), you create a vector of variant types (yuck), or you create a some kind of variant vector (yuck). I'd probably choose the latter because its the simplest method that still has type safe vectors within it.

typedef union VectorOfThings {
    std::vector<int> i;
    std::vector<double> d;
    std::vector<string> s;
};
struct TypedVectorOfThings {
    enum Type { ints, doubles, strings };
    Type type;
    VectorOfThings myVector;
};
std::vector<TypedVectorOfThings > myVectorOfVectors;

和调味季节.但是,天哪,这太可怕了.这是用户定义的数据库表的运行时表示形式还是什么?

and season to taste. But oh dear gods it's horrible. Is this a run time representation of a user defined database table or something?

这篇关于如何在C ++中获得不同向量的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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