Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::vectors of vectors解除分配其共享内容(堆栈实体对象的不正确使用?)_C++_Stack_Shared Ptr_Smart Pointers_Stdvector - Fatal编程技术网

C++ std::vectors of vectors解除分配其共享内容(堆栈实体对象的不正确使用?)

C++ std::vectors of vectors解除分配其共享内容(堆栈实体对象的不正确使用?),c++,stack,shared-ptr,smart-pointers,stdvector,C++,Stack,Shared Ptr,Smart Pointers,Stdvector,共享_ptr的向量向量解除分配其片段实例。 我在我的Board类中创建了它,如下所示: std::vector < std::vector < std::shared_ptr <Piece> > > board; 以下是董事会类别代码: #include "Board.h" #include <iostream> using namespace std; Board::Board(int width_, int height_) : wid

共享_ptr的向量向量解除分配其片段实例。 我在我的Board类中创建了它,如下所示:

std::vector < std::vector < std::shared_ptr <Piece> > > board; 
以下是董事会类别代码:

#include "Board.h"
#include <iostream>

using namespace std;

Board::Board(int width_, int height_) : width(width_), height(height_), board(height)
{
    cout << "Board::board with capacity " << board.capacity() << " size: " << board.size() << endl;
//    board = vector< vector < shared_ptr < Piece >>> (height) ;

    for (int i=0; i<height; i++)
    {
        vector < shared_ptr < Piece > > row(width);
        board[i] = row;

        for (int j=0;j<width;j++) {
            shared_ptr< Piece > piece = make_shared<Piece>();
            row[j] = piece;
            cout << "Row " << j << " Column " << i << " piece" << &*piece << endl;
        }
    }
}
#包括“Board.h”
#包括
使用名称空间std;
线路板::线路板(内部宽度、内部高度):宽度、高度、线路板(高度)
{
库特
这将在分配任务时将
向量的内容复制到
板[i]
中。然后,您继续填充本地
向量,但不再对其执行任何操作

将分配移动到循环之后:

for (int i=0; i<height; i++)
{
    vector < shared_ptr < Piece > > row(width);

    for (int j=0;j<width;j++) {
        shared_ptr< Piece > piece = make_shared<Piece>();
        row[j] = piece;
        cout << "Row " << j << " Column " << i << " piece" << &*piece << endl;
    }
    board[i] = std::move(row);
}
如果您想进一步简化代码,可以这样做(我甚至会说它更清晰):


for(int i=0;ioh,谢谢!:)我来自Objective C的世界,这里的一切都是指针,所以顺序根本不重要。感谢你的回答,现在我很明显,行堆栈对象的内容被复制了,所以有两个单独的实例(我只处理本地堆栈,而不是向量中的一个)。我开始质疑这种方法的内存效率(不喜欢产生太多实例的想法)。当然,我总是可以使用原始指针来堆实例,但这打破了使用堆栈对象/RAII来管理资源的整个想法。有什么方法可以两全其美?@barney如果我理解正确,那么“两全其美”是我的别名代码-
将仅是对
中向量对象的引用。我想是的。Afaik对向量的引用已经初始化,并且在堆中有自己的元素(0个元素)所以行。resize()只需在堆中为x shared_ptr创建足够的空间,然后允许通过索引(运算符[])访问它们,对吗?但是共享指针呢?它们是在调整大小时创建的,每次我执行row[j]=piece;都会复制一些内存。有什么方法可以避免这种复制(好像我会使用pPiece=new piece();唯一正在移动的内存是它的指针(指针数据的4/8字节),没有任何共享_ptr对象的开销fields@barney添加了一个更加精简的版本。哇!这看起来非常优雅和高效(根据emplace_back()方法描述)谢谢。它允许完美地转发参数并将对象直接构造到容器中,而无需临时堆栈对象!
class Piece {
public:
    Piece() { std::cout << "Piece born " << this << std::endl;}
    ~Piece() { std::cout << "Piece death " << this << std::endl;}
    Piece(const Piece &rhs) {
        std::cout << "Piece copy " << this << " from " << &rhs << std::endl;
    }
    Piece & operator=(const Piece & rhs) {
        std::cout << "Piece assigned " << this << " from " << &rhs << std::endl;
        return *this;
    }
};
board[i] = row;
for (int i=0; i<height; i++)
{
    vector < shared_ptr < Piece > > row(width);

    for (int j=0;j<width;j++) {
        shared_ptr< Piece > piece = make_shared<Piece>();
        row[j] = piece;
        cout << "Row " << j << " Column " << i << " piece" << &*piece << endl;
    }
    board[i] = std::move(row);
}
for (int i=0; i<height; i++)
{
    auto &row = board[i];
    row.resize(width);

    for (int j=0;j<width;j++) {
        shared_ptr< Piece > piece = make_shared<Piece>();
        row[j] = piece;
        cout << "Row " << j << " Column " << i << " piece" << &*piece << endl;
    }
}
for (int i=0; i<height; i++)
{
    auto &row = board[i];

    for (int j=0;j<width;j++) {
        row.emplace_back(make_shared<Piece>());
        cout << "Row " << j << " Column " << i << " piece" << row.back().get() << endl;
    }
}