简体   繁体   English

固定大小 std::vector

[英]Fixed size std::vector

I'm currently in the process of changing the way I access my data structures, and I'm reviewing one of two solutions in the general case for vectors of vectors.我目前正在改变我访问数据结构的方式,并且我正在审查向量向量的一般情况下的两种解决方案之一。

My incentive is simple;我的动机很简单; I want cache locality without giving up my interfaces.我想要缓存位置而不放弃我的接口。

I know the max-size of my vectors at compile time, but they won't always be reaching the max.我在编译时知道我的向量的最大大小,但它们不会总是达到最大值。 Common case is about 80%, and the total size of each vector is to be relatively small. Common case是80%左右,每个vector的总大小要比较小。 If I reach that max, I have made an error in logic somewhere, and want it to throw an error.如果我达到那个最大值,我在某个地方犯了逻辑错误,并希望它抛出错误。

The first solution that came to mind, was to use a Pool allocator with std::vector, seemed like a good idea, but maybe a bit messy;想到的第一个解决方案是使用带有 std::vector 的池分配器,这似乎是个好主意,但可能有点混乱; having not used allocators properly before, I wasn't too sure of the solution.之前没有正确使用分配器,我不太确定解决方案。 I'm not much of a fan for storing the data separate to its owners, and I want the implementation to be as transparent as possible.我不太喜欢将数据与其所有者分开存储,我希望实现尽可能透明。

The second solution works great for me at the moment, but I want to make it a little bit less in line.第二种解决方案目前对我来说效果很好,但我想让它少一些。 At the moment, it is thus:目前,它是这样的:

class Foo {
public:
        std::array<Bar, 10> bars;
        size_t used;

        // std::vector<Bar> bars; // reserved to 10... maybe

        void add(int var1, int var2) {
                if (used >= bars.size()) throw "Error";
                bars[used] = Bar(var1, var2);
                ++used;

                // std::vector alternative
                // bars.push_back(Bar(var1, var2));
        }
        void remove(size_t idx) {
                bars[idx] = bars.back();
                --used;

                // bars.back().~Bar(); // should happen, not sure if safe

                // std::vector alternative
                // bars[idx] = bars.back();
                // bars.pop_back();
        }
}

Which, as mentioned, works great.如前所述,效果很好。 However, if I wanted to move this solution elsewhere, I'd rather not have to implement it again, and have proper semantics in terms of destruction (similar to that of an actual vector).但是,如果我想将这个解决方案移到别处,我宁愿不必再次实现它,并且在破坏方面具有适当的语义(类似于实际向量的语义)。

So, I was wondering what a good solution could be?所以,我想知道什么是好的解决方案? Currently, I've started wrapping an std::array , but it is starting to get messy, and I'm sure this is solved problem.目前,我已经开始包装一个std::array ,但它开始变得混乱,我确信这已经解决了问题。

See this link for several options for fixed-size data structures with a vector-like interface.有关具有类似向量接口的固定大小数据结构的几个选项,请参阅此链接 In particular, boost::auto_buffer and eastl::fixed_vector seem viable options for your code.特别是, boost::auto_buffereastl::fixed_vector似乎是您代码的可行选择。

Yet another alternative is to use Hinnant's stack allocator with the regular std::vector , but MSVC++ has some trouble compiling it.另一种选择是将 Hinnant 的堆栈分配器与常规std::vector一起使用,但 MSVC++ 在编译它时遇到了一些麻烦。

Boost has a MultiArray class intended for this purpose. Boost 有一个用于此目的的 MultiArray 类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM