我目前正在尝试将动态的脚步音频合并到我的游戏中。这里是一些代码:
class MyClass
{
vector< unique_ptr <Sound> > footstep_a;
vector< unique_ptr <Sound> > footstep_b;
vector< unique_ptr <Sound> > footstep_c;
vector< Sound > currentfootsteps;
}
因此,基本上我想做的就是将footstep_ vector 之一分配给currentfootsteps,这样我就可以拥有:
if( walkingarea == a )
currentfootsteps = a;
else ......
我已经尝试过执行以下操作,但它只会在 vector 等方面引发一百万个错误:
if ( walkingarea == a )
currentfootsteps.clear();
for(int i = 0; i < footstep_a.size(); i++)
currentfootsteps.push_back( footstep_a[i] );
谁能帮我?
最佳答案
我不太了解您要做什么,但是假设Sound
类是可复制的,则可以编译:
currentfootsteps.clear();
for(auto const& up : footstep_a) {
currentfootsteps.push_back(*up);
}
请注意,您正在复制
footstep_a
中的每个元素,并将其添加到currentfootsteps
中。如果
Sound
是仅移动的,或者您想避免复制,请改用此方法:currentfootsteps.clear();
for(auto&& up : footstep_a) {
currentfootsteps.push_back(std::move(*up));
}
但是似乎您也可以通过使
currentfootsteps
成为指针,并根据满足的条件简单地指向vector
之一来避免所有这些情况。vector< unique_ptr <Sound> > *currentfootsteps = nullptr;
if ( walkingarea == a ) {
currentfootsteps = &footstep_a;
} else if ...
关于c++ - 将unique_ptr vector 分配给 vector C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23419100/