簡體   English   中英

保留 3D 向量中的元素 c++

[英]Reserving elements in a 3D vector c++

我有一個 3d 容器:

 std::vector<int, std::vector<std::vector<double>>> myVec;

我想要尺寸 [n+1][3*(n+10)+1][16*(n+10)+1]。 我想快速保留向量中的這個空間,並且沒有太多其他意大利面條函數,這樣我就可以模擬動態 3D 數組。 我該怎么做?

預定還是尺寸?

http://www.cplusplus.com/reference/vector/vector/reserve/ http://www.cplusplus.com/reference/vector/vector/resize/

使用構造函數調整大小的演示:

#include <iostream>
#include <vector>
using namespace std;

template <class T>
void printSize(T v) {
    cout<<v.size()<<endl;
}
int main(){
    int n = 1;//Initial n value;
    int d1 = n+1, d2 = 3+(n+10)+1, d3 = 16*(n+10)+1;
    vector<vector<vector<int>>> i3D(d1,vector<vector<int>>(d2,vector<int>(d3)));
    //i3D.resize(d1,vector<vector<int>>(d2,vector<int>(d3)));
    printSize(i3D);
    printSize(i3D[0]);
    printSize(i3D[0][0]);
    return 0;
}

在我的示例中,我使用構造函數調整了大小。 您可以使用調整大小方法執行相同操作。
另一方面,保留將需要(據我所知)使用循環並為每個數組(全部)保留內存。

將其視為調整大小的問題,因為您指定了 x、y 和 z 維度的預設大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM