C++ 从向量中删除唯一的_ptr并将其用作新结构的参数
移动std::unique_ptr的语法让我有些困惑,至少我找不到一个明确的答案,关于如何移动unique_ptr 我有一些堆分配的节点,并希望创建新的节点,其中有两个已经存在的节点作为子节点。反过来,它们应该被插入到向量中C++ 从向量中删除唯一的_ptr并将其用作新结构的参数,c++,vector,unique-ptr,C++,Vector,Unique Ptr,移动std::unique_ptr的语法让我有些困惑,至少我找不到一个明确的答案,关于如何移动unique_ptr 我有一些堆分配的节点,并希望创建新的节点,其中有两个已经存在的节点作为子节点。反过来,它们应该被插入到向量中 #include <memory> #include <vector> template <typename T> struct Node{ std::unique_ptr<Node<T>> left,
#include <memory>
#include <vector>
template <typename T>
struct Node{
std::unique_ptr<Node<T>> left, right;
Node(std::unique_ptr<Node<T>> left, std::unique_ptr<Node<T>> right){
this->left = left;
this->right = right;
}
}
template <typename T>
void foo(){
std::vector<std::unique_ptr<Node<T>>> vec;
//...
vec.insert(vec.begin() + idx, std::unique_ptr<Node<T>>(new Node<T>(vec.at(vec.begin() + idx), vec.at(vec.begin() + idx + 1))));
}
我只得到错误消息,没有找到匹配的函数调用
expression.hpp:69:58: error: no matching function for call to ‘std::vector<std::unique_ptr<Node<int>, std::default_delete<Node<int> > >, std::allocator<std::unique_ptr<Node<int>, std::default_delete<Node<int> > > > >::at(__gnu_cxx::__normal_iterator<std::unique_ptr<Node<int>, std::default_delete<Node<int> > >*, std::vector<std::unique_ptr<Node<int>, std::default_delete<Node<int> > >, std::allocator<std::unique_ptr<Node<int>, std::default_delete<Node<int> > > > > >)’
是否有人能帮助我,或者有什么想法,我可以在哪里找到正确的语法,以及我应该使用哪个移动/复制功能
看一看at的声明。参数类型是size\u类型,是整数类型。您正在尝试传递迭代器
看一看at的声明。参数类型是size\u类型,是整数类型。您试图传递一个迭代器。听起来您想合并向量中的两个相邻节点。您需要分多个步骤执行此操作
template <typename T>
struct Node{
std::unique_ptr<Node<T>> left, right;
Node(std::unique_ptr<Node<T>> left, std::unique_ptr<Node<T>> right)
: left(std::move(left)),
right(std::move(right))
{}
}
template <typename T>
void foo(){
std::vector<std::unique_ptr<Node<T>>> vec;
//...
// Identify first element
auto first = vec.begin() + idx;
// and second element
auto second = first + 1;
// make the merged node
auto merged = std::make_unique<Node<T>>(std::move(*first), std::move(*second));
// remove the now empty Node pointers - note that this relies on the adjacency of first and second
auto pos = vec.erase(first, first + 2);
// add the new node to the correct place
vec.emplace(pos, std::move(merged));
}
听起来像是要合并向量中的两个相邻节点。您需要分多个步骤执行此操作
template <typename T>
struct Node{
std::unique_ptr<Node<T>> left, right;
Node(std::unique_ptr<Node<T>> left, std::unique_ptr<Node<T>> right)
: left(std::move(left)),
right(std::move(right))
{}
}
template <typename T>
void foo(){
std::vector<std::unique_ptr<Node<T>>> vec;
//...
// Identify first element
auto first = vec.begin() + idx;
// and second element
auto second = first + 1;
// make the merged node
auto merged = std::make_unique<Node<T>>(std::move(*first), std::move(*second));
// remove the now empty Node pointers - note that this relies on the adjacency of first and second
auto pos = vec.erase(first, first + 2);
// add the new node to the correct place
vec.emplace(pos, std::move(merged));
}
请包含一个和来自语义POV的错误消息:在操作后,谁应该是唯一实例的所有者?std::vector的at成员函数从技术上讲是一个整数,而不是一个迭代器,因此我认为您可以在对at的调用中直接使用idx和idx+1。这不是关于语法,而是关于类型。我怀疑您的错误消息也这么说。所有者应该是新节点@πάνταῥεῖ@user463035818这样做了,希望这是更好的请包含a和来自语义POV的错误消息:在操作后,谁应该是唯一实例的所有者?std::vector的at成员函数从技术上讲是一个整数,而不是迭代器,因此我认为您可以在对at的调用中直接使用idx和idx+1。这不是关于语法,而是关于类型。我怀疑您的错误消息也这么说。所有者应该是新节点@πάνταῥεῖ@用户463035818做到了,希望这是更好的足够了这已经为我工作过了。。。但我会调查的;thxokay找到了对我有用的地方。虽然不在,但很奇怪,这以前对我很有效。。。但我会调查的;thxokay找到了对我有用的地方。不在但为“`erease``+1这将解决空nullptr节点的问题,Clebo Sevic提到他不希望在向量中包含空nullptr节点。如何将左和右分配给节点的成员?=是一个已删除的函数。我应该再次移动吗?@CleboSevic我回答中的代码会在需要时移动。一般来说,成员初始值设定项比在构造函数主体中编写代码更可取。+1这将解决空nullptr节点的问题,Clebo Sevic提到他不希望在向量中包含空nullptr节点。如何将左和右分配给节点的成员?=是一个已删除的函数。我应该再次移动吗?@CleboSevic我回答中的代码会在需要时移动。一般来说,成员初始值设定项比构造函数体中的代码更可取。