Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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++ 如何复制向量<;独特的_ptr<;T>&燃气轮机;到独立向量<;T*>;_C++_C++11_Vector_Smart Pointers_Unique Ptr - Fatal编程技术网

C++ 如何复制向量<;独特的_ptr<;T>&燃气轮机;到独立向量<;T*>;

C++ 如何复制向量<;独特的_ptr<;T>&燃气轮机;到独立向量<;T*>;,c++,c++11,vector,smart-pointers,unique-ptr,C++,C++11,Vector,Smart Pointers,Unique Ptr,我有一个std::vector vec1,其中T是一个抽象类型。我想创建std::vector vec2,其中第二个向量指针指向的对象是第一个向量指针指向的对象的副本 例如:*(vec1[0])==*(vec2[0])和vec1[0].get()vec2[0]。。。等等 如何做到这一点?手动方式: std::vector<std::unique_ptr<T>> vec1; std::vector<T*> vec2; vec2.reserve(vec1.size

我有一个
std::vector vec1
,其中T是一个抽象类型。我想创建
std::vector vec2
,其中第二个向量指针指向的对象是第一个向量指针指向的对象的副本

例如:
*(vec1[0])==*(vec2[0])
vec1[0].get()vec2[0]
。。。等等

如何做到这一点?

手动方式:

std::vector<std::unique_ptr<T>> vec1;
std::vector<T*> vec2;
vec2.reserve(vec1.size()); // optimization to avoid reallocations

for (const auto& e : vec1) {
    vec2.push_back(e->clone());
}
std::vec1;
std::vec2载体;
vec2.reserve(vec1.size());//优化以避免重新分配
用于(常数自动和e:vec1){
vec2.向后推(e->clone());
}
使用
virtualt*T::clone()const
手动方式:

std::vector<std::unique_ptr<T>> vec1;
std::vector<T*> vec2;
vec2.reserve(vec1.size()); // optimization to avoid reallocations

for (const auto& e : vec1) {
    vec2.push_back(e->clone());
}
std::vec1;
std::vec2载体;
vec2.reserve(vec1.size());//优化以避免重新分配
用于(常数自动和e:vec1){
vec2.向后推(e->clone());
}

使用
std::transform

std::vector<T*> vec2;
vec2.reserve(vec1.size()); // optimization to avoid reallocations, it isn't necessary, and without it the code still works correctly
std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2), [](const std::unique_ptr<T>& p){ return YourCloneFunction(*p); }
这样,代码就变得

std::vector<T*> vec2;
vec2.reserve(vec1.size()); // optimization to avoid reallocations, it isn't necessary, and without it the code still works correctly
std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2), [](const std::unique_ptr<T>& p){ return p->clone().release(); }
std::vectorvec2;
vec2.reserve(vec1.size());//为了避免重新分配,没有必要进行优化,没有优化,代码仍然可以正常工作
std::transform(vec1.begin(),vec1.end(),std::back_inserter(vec2),[](const std::unique_ptr&p){return p->clone().release();}
请注意,我们有
vec2
原始指针指向不属于任何智能指针的对象。这是不好的,除非您将
vec2
传递到拥有这些指针的旧函数中


否则,如果您只需要副本的
std::vector
视图,请克隆到中间
std::vector
,然后使用
std::transform
将每个实例上的
.get()
结果复制到
std::vector

std::vector<T*> vec2;
vec2.reserve(vec1.size()); // optimization to avoid reallocations, it isn't necessary, and without it the code still works correctly
std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2), [](const std::unique_ptr<T>& p){ return YourCloneFunction(*p); }
这样,代码就变得

std::vector<T*> vec2;
vec2.reserve(vec1.size()); // optimization to avoid reallocations, it isn't necessary, and without it the code still works correctly
std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2), [](const std::unique_ptr<T>& p){ return p->clone().release(); }
std::vectorvec2;
vec2.reserve(vec1.size());//优化为了避免重新分配,这是不必要的,没有它,代码仍然可以正常工作
std::transform(vec1.begin(),vec1.end(),std::back_inserter(vec2),[](const std::unique_ptr&p){return p->clone().release();}
请注意,我们有
vec2
原始指针指向不属于任何智能指针的对象。这是不好的,除非您将
vec2
传递到拥有这些指针的旧函数中



否则,如果只需要副本的
std::vector
视图,请克隆到中间
std::vector
,然后复制
.get()的结果
在每个实例上指向
std::vector

到底是什么问题?您不知道如何从
unique\u ptr
?或其他方法获取底层指针?@SergeyA。我知道从
std::unique\u ptr
获取原始指针很热(
get()
方法)。但如果我将其推送到vec2,那么vec2将不会独立于vec1。vec1中的指针将指向内存中与vec2中的指针相同的位置。我需要对象的副本。为什么?​​​​​​​​​​​ 这是一个巨大的代码气味。C API需要
T**
和所有权:(@BarryTheHatchet是的,我知道这很糟糕,但我需要这个“黑客”(在ut测试中)。我打赌你真的不需要:)到底是什么问题?您不知道如何从
unique\u ptr
?或其他什么地方获取底层指针?@SergeyA。我知道从
std::unique\u ptr
获取原始指针很有意思(
get()
方法)。但如果我将其推送到vec2,那么vec2将不会独立于vec1。vec1中的指针将指向内存中与vec2中的指针相同的位置。我需要对象的副本。为什么?​​​​​​​​​​​ 这是一个巨大的代码气味。C API需要
T**
和所有权:(@BarryTheHatchet是的,我知道这很糟糕,但我需要这个“hack”(在ut测试中)。我打赌你真的不需要:)因为它是一个副本,你应该表现出良好的行为,在测试之前调用适当大小的reserveloop@galop1n,没有实际需要。“T是抽象类型”,这意味着您不能构造
t
本身的新实例。@SergeyA:不需要,但完全合适。因为它是一个副本,您应该表现出良好的行为,在loop@galop1n,没有实际需要。“T是抽象类型”,这意味着你不能构造
t
本身的新实例。@SergeyA:没有必要,但完全合适。因为它是一个副本,你应该表现出良好的行为,在转换之前调用适当大小的reserve。你为什么要屈从于这个要求?这里确实没有理由调用
reserve
。@milleniumbug.谢谢。因为它是我的It’这是一个副本,你应该表现出良好的行为,在转换之前调用适当大小的reserve。你为什么要屈从于这个需求?这里真的没有理由调用
reserve
。@milleniumbug。谢谢。