1

当使用原始指针时,我能够做到这一点:

std::vector< Vector3* > vertices;
Vector3* v = new Vector3[amount];
fread(v, sizeof(Vector3), amount, file);
for ( int i = 0; i < amount; ++i )
    vertices.push_back( &v[i] );

现在,如何对智能指针做同样的事情?我尝试过这样的事情:

std::vector< std::unique_ptr< Vector3 > > vertices;
std::unique_ptr< Vector3 >* v = new std::unique_ptr< Vector3 >[amount];
fread(v, sizeof(Vector3), amount, file);
for ( int i = 0; i < amount; ++i )
    vertices.push_back( std::move( v[i] ) );
delete [] v;

它有什么意义吗?它可以编译,并且可以工作,但恐怕最后一条指令(delete [] v;)是正确的吗?

4

1 回答 1

2

别紧张

std::vector<Vector3> vertices(amount);
assert(!vertices.empty());
fread(&vertices[0], sizeof(Vector3), amount, file);
于 2013-09-21T11:25:06.217 回答