簡體   English   中英

如何推回向量<vector<object> &gt; 其中我只保留了memory? </vector<object>

[英]How do I push back to a vector<vector<Object>> in which I have only reserved memory?

所以我有一個成員vector<vector<Foo>> _meshMatrix如果我這樣做

_meshMatrix.resize(_numRefElementsPerRow);
    for(unsigned int i = 0; i < _numRefElementsPerRow ; i++)
        _meshMatrix [i].resize(_numRefElementsPerColumn);

然后我有一個Foo對象矩陣,我可以使用[][]訪問它的元素

for(unsigned int row = 0; row < _numRefElementsPerRow; row++)
    for(unsigned int column = 0; column < _numRefElementsPerRow; column++)
        _meshMatrix [row][column] = someFooObject;

而不是保存 memory 因為某些矩陣位置不會被填滿,我想使用reservepush_back ,所以

_meshMatrix.reserve(_numRefElementsPerRow);
    for(unsigned int i = 0; i < _numRefElementsPerRow ; i++)
        _meshMatrix [i].reserve(_numRefElementsPerColumn);

但我不知道如何 push_back 兩次進入這個矩陣

for(unsigned int row = 0; row < _numRefElementsPerRow; row++)
    for(unsigned int column = 0; column < _numRefElementsPerRow; column++)
        //_meshMatrix.??? = someFooObject; // how dO I use push_back here?

任何想法?

_meshMatrix [row].push_back(value)

僅供參考,您正在使用_numRefElementsPerRow來遍歷行和列並為列保留空間。 除非它們的大小相同,否則這是一個錯誤。 我認為您打算使用_numRefElementsPerColumn


編輯:似乎我對您的代碼做出了不正確的假設。 我會瀏覽你的代碼。 EDIT2:我不應該在深夜這樣做。 打字保留 -> 調整大小。 固定的。

vector<vector<Foo>> _meshMatrix;
_meshMatrix.reserve(_numRefElementsPerRow);

調用 resize default 會初始化vector<Foo>_numRefElementsPerRow元素。 memory 分配並構建。

在這里調用 reserve 會分配足夠的 memory 來保存vector<Foo>_numRefElementsPerRow元素。 這並不意味着已經構建了任何元素; 您只確保行向量中有足夠的空間容納那么多。 請注意,我將其稱為行向量,因為您通過 _numRefElementsPer Row將其分配為保存一行中的元素數

    for(unsigned int i = 0; i < _numRefElementsPerRow ; i++)
        _meshMatrix [i].reserve(_numRefElementsPerColumn);

接下來,為每列保留足夠的 memory。 但是,您錯過了一步。 這是未定義的行為。 您正在對尚未構造的對象調用resize 如果您要調用_meshMatrix.size()它將返回0 要創建對象,您必須構造它們,例如這是有效的:

    vector<vector<Foo>> _meshMatrix;
    _meshMatrix.reserve(_numRefElementsPerRow);
    for(unsigned int i = 0; i < _numRefElementsPerRow ; i++) {
        _meshMatrix.push_back(); // <<< construct a vector<Foo> at the end
        _meshMatrix[i].reserve(_numRefElementsPerColumn);

現在如果你想為每一列添加元素,你可以這樣做:

for (unsigned j = 0; j < _numRefElementsPerColumn; j++) {
    _meshMatrix[j].push_back(value); // <<< some value
}

把它放在一起:

    // Create empty vector for rows
    vector<vector<Foo>> _meshMatrix;
    // Allocate memory ahead of time for the elements
    _meshMatrix.reserve(_numRefElementsPerRow);

    for(unsigned int i = 0; i < _numRefElementsPerRow ; i++) {
        // Create a vector for column values
        _meshMatrix.emplace_back(); // or _meshMatrix.push_back({});
        // Allocate memory ahead of time for the elements
        _meshMatrix[i].reserve(_numRefElementsPerColumn);
        for (unsigned j = 0; j < _numRefElementsPerColumn; j++) {
            // Add values
            _meshMatrix[j].push_back(value); // <<< some value
        }
    }

你可以像這樣 push_back

for(unsigned int row = 0; row < _numRefElementsPerRow; row++)
{
    vector<Foo> meshMatrixCols;
    for(unsigned int column = 0; column < _numRefElementsPerRow; column++)
         meshMatrixCols.push_back(someObject); // how dO I use push_back here?

     _meshMatrix.push_back(meshMatrixCols);
}

暫無
暫無

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

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