English 中文(简体)
我如何在C++实施一个可变的长老储存?
原标题:How can I implement a variable length row-based storage in C++?

我正试图在C++实施一种可变的以增长为基础的储存系统。 想法是,这一储存系统中的每一行都应能够持有不同长度和不同类型的数据。 例如,第一行可以是[1,8.6,“hello”,第二行可以是[2,9.6]。

为此,我最初计划使用每行都有单程模板的标记。 每一行将作为<代码>std:tuple<Types...>储存,整个储存将作为<代码>std:vector<std:tuple<Types...>>。

然而,我面临的问题是,当我试图宣布一个地图级以存放这种储存时,我需要明确宣布其参数类型——例如:<代码>Map<int, 双重:string >。 这种做法限制我把不同长度和类型的记录插入同样的地图。

在这里,可以简单地展示我的观点:

template <typename... Types>
class Tuple {
private:
    std::tuple<Types...> data;
public:
    Tuple() : data() {}

    Tuple(Types &&... args) : data(std::forward<Types>(args)...) {}
}

template <typename... Types>
class Map {
private:
    vector<Tuple<Types...>> record;
public:
    void addRow(Types &&... args) {
        record.emplace_back(std::forward<Types>(args)...);
    }
};

是否有办法建立一个地图类别,每个浏览量可以持有不同的数据长度和类型? 我如何在C++中落实这一点?

问题回答

你们可以使用C++的辛醇来描述这种结构。 C++中的类型定义一旦编制方案,就将确定有关物体储存在石块中的代表性。

这并不意味着这是可能的。 你有两种选择:

  1. 采用多变方式建立动态结构。 见Proxy,Fly 体重模式。

  2. If you REALLY need a contiguous structure (stream) of data in memory, you re on a long and thorny path to create own memory pool or protobuf library. can you use one of those existing ones?





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签