如何从 std::deque 高效创建 D3D 缓冲区

How to efficiently create D3D Buffer from std::deque

众所周知,std::deque< T > 由数据块组成(数组数组或数组列表,我不知道)。如果 T 的大小足够小,那么通常创建的每个块的大小为 4k。对于(我相信)libc++libstdc++ 是正确的(如果我错了请纠正我)。 4k 是 x86/x64 平台的页面大小(我相信)。

我将数据一一采集,存入std::deque。最后我应该使用它的 API.

将它传递给 DirectX 缓冲区

我能否获得对 std::deque 的各个块的访问权限,以便一次加载它们(没有实现细节字段),但不能一个接一个地加载 T 类型的值?

要是有这样一个std::deque的接口就好了,可以实现内在的想要的。比如说,std::unordered_{set,map} 有桶(w/o 访问它们)。新开发的尝试设计允许将单个节点从一个容器移动到另一个容器等。

你自己用指针算法检查块怎么样?这是一个填充 std::deque 的示例,然后将迭代器提供给每个块的开头:

#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>

int main()
{
    typedef int MyType;
    std::deque<MyType> myInts(10000);
    //fill the array
    int count = 0;
    std::generate(myInts.begin(), myInts.end(), [&](){ return ++count; });

    //chunks' beginnings container
    std::vector<std::deque<MyType>::iterator> myIntsChunks;
    myIntsChunks.push_back(myInts.begin());
    for(std::deque<MyType>::iterator it = myInts.begin()+1; it != myInts.end(); it++)
    {
        if(&(*(it-1)) != ((&(*(it)))-1)) //if this element's pointer isn't right after the previous element's pointer
        {
            myIntsChunks.push_back(it);
        }
    }
    std::cout<<"I found " << myIntsChunks.size() << " chunk(s)." << std::endl;
    std::for_each(myIntsChunks.begin(), myIntsChunks.end(),
                  [&myInts](const std::deque<MyType>::iterator& it)
    {
        std::cout<<"Chunk starts at " << std::distance(myInts.begin(),it) << std::endl;
    });
}

如果您将 std::deque 更改为 std::vector,您将只会获得一个区块!