[英]C++ new pointer from pointer to pointer?
I have a template linkedList that I would like to dynamically create "head" pointers for... 我有一个模板linkedList,我想为...动态创建“头”指针。
I seem unable to get any syntax to work.. my best guess is: 我似乎无法使用任何语法。.我的最佳猜测是:
linkedList<int>** ptr;
ptr = new (linkedList<int>*)[1];
But it doesn't work. 但这是行不通的。 I'm fairly new to C++ so any help is appreciated!
我对C ++还是相当陌生,因此不胜感激! Thanks!
谢谢!
To get a pointer, do: 要获取指针,请执行以下操作:
T* ptr = new T;
where T
is your type. 其中
T
是您的类型。
For a pointer-to-pointer, do: 对于指针到指针,请执行以下操作:
T** ptrptr = new T*;
allocating the space for one pointer, which still needs to be filled like the first method: 为一个指针分配空间,仍然需要像第一个方法一样填充:
*ptrptr = new T;
Now you got a valid pointer-to-pointer. 现在,您获得了有效的指针对指针。
Is there some reason you are not using std::list? 您是否由于某些原因未使用std :: list? (or std::forward_list)
(或std :: forward_list)
Check out the header files for std::list, or your nearest C++ book, or in fact cppreference.com 签出std :: list的头文件,或者离您最近的C ++书籍,或者实际上是cppreference.com
Your linked list class template should have a function to return the head of the list. 您的链接列表类模板应具有返回列表头的功能。 Look at std::list::begin() in your compiler's c++ library.
查看编译器c ++库中的std :: list :: begin()。 The std::list::iterator type is a pointer to whatever goes in the list.
std :: list :: iterator类型是指向列表中所有内容的指针。 (ie T*)
(即T *)
Though I'm not sure pointer array is really needed for your linked list, as for just new
construct, the following form will be compiled. 虽然我不确定您的链表中确实需要指针数组,但对于
new
结构,将编译以下形式。
ptr = new (linkedList<int>*[1]);
EDIT : 编辑 :
This allocates pointer array: 这分配了指针数组:
linkedList<int>** ptr = new (linkedList<int>*[1]);
This allocates array: 这分配数组:
linkedList<int>* ptr = new linkedList<int>[1];
This allocates one element: 这分配了一个元素:
linkedList<int>* ptr = new linkedList<int>;
Normally the head of a linked list would look something like: 通常,链接列表的标题如下所示:
node<int> *head = NULL;
When you want to create and insert a node, you'd use something like: 当您要创建并插入节点时,可以使用以下方法:
insert(node<int> *&list, int value) {
// insert new node containing `value` at head of `list`.
node<int> *temp = new node(value);
temp->next = list;
list=temp;
}
You could use this something like: 您可以这样使用:
node<int> *list = NULL;
for (int i=0; i<10; i++)
insert(list, i);
Of course, unless this is for homework (or something on that order), you should stop working on this immediately, and just std::list
(or boost::slist
, if you want a singly-linked list). 当然,除非这是用于家庭作业(或按此顺序排列的东西),否则应立即停止处理,而只需停止
std::list
(或boost::slist
,如果需要单链接列表)。
Edit: I'm adding more detail mentioned by the OP in comment. 编辑:我添加了OP在评论中提到的更多细节。 For the moment, the
avl_tree::insert
does not attempt to maintain balance. 目前,该
avl_tree::insert
并不试图保持平衡。 It's just a plain-jane un-balanced insert, but it should be adequate to demonstrate what we care about at the moment. 这只是一个普通的不平衡插入,但足以证明我们目前关心的是什么。
template <class T>
struct linked_list {
node *head;
void insert(T v) {
node<T> *n = new node(v, head);
head = n;
}
linked_list() : head(NULL) {}
template <class T>
struct node {
node *next;
T data;
node(T const &v, node *n=NULL) : data(v), next(n) {}
};
};
template <class keyType, class dataType>
class avl_tree {
struct node {
node *left, *right;
char balance;
keyType key;
dataType data;
node(keyType const &k, dataType const &d)
: left(NULL), right(NULL), balance(0), key(k), data(d)
{ }
bool operator<(node const &other) {
return key < other.key;
}
} *root;
bool insert(node const *new_node, node *&tree) {
if (tree == NULL) {
tree = new_node;
return true;
}
else if (*new_node < *tree)
return insert(new_node, tree->left);
else if (*new_node > *tree)
return insert(new_node, tree->right);
else // new_node == tree
return false; // couldn't insert -- already present.
}
public:
avl_tree() : root(NULL) {}
void insert(keyType const &key, dataType const &data) {
node *temp = new node(key, data);
insert(temp, root);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.