简体   繁体   English

将项目从一个向量复制到另一向量

[英]Copy items from one vector to another vector

I'm trying to copy certain items from one vector into another vector which contains shared_ptr objects. 我正在尝试将某些项目从一个向量复制到另一个包含shared_ptr对象的向量。

I don't want a reference but a unique copy of that object placed into the other vector. 我不希望引用,而是将该对象的唯一副本放入另一个向量中。 The whole point of this is to fill listEnvironmentStatic with game object and when the game map starts, everything gets copied to listEnvironment for simulations and when the players wants to reset the map, listEnvironment copies everything from listEnvironmentStatic once again and every object is back to its original location. 整个过程的重点是将游戏对象填充到listEnvironmentStatic中,当游戏地图开始时,所有内容都会复制到listEnvironment中进行模拟,并且当玩家想要重置地图时,listEnvironment会再次从listEnvironmentStatic中复制所有内容,并且每个对象都返回其状态。原始位置。

    this->listEnvironment.insert(this->listEnvironment.end(), this->listEnvironmentStatic.begin(), this->listEnvironmentStatic.end());

using the std::copy and resizing the second vector does't work either. 使用std :: copy并调整第二个向量的大小也不起作用。

Look a the following example: 看下面的例子:

std::vector<std::shared_ptr<Environment>> listEnvironmentStatic;
std::vector<std::shared_ptr<Environment>> listEnvironment;

Now lets say i have a couple of items in listEnvironmentStatic that i want to copy over to listEnvironment (which always contains at least 1 object), how would I do unique copy and still keep the listEnvironmentStatic in case the player wants to restart the map once again? 现在,假设我要复制到listEnvironment(始终包含至少一个对象)中的listEnvironmentStatic中有几个项目,我将如何进行唯一复制,并且在玩家想要重新启动地图的情况下仍保留listEnvironmentStatic再次?

If you want new instances which are copies from the originating vector, you could use: 如果您希望新实例是原始矢量的副本,则可以使用:

for ( const auto& e : listEnvironmentStatic )
    listEnvironment.push_back( std::make_shared<Environment>( *e ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM