堆中 class 中的结构变量和智能指针的使用
Struct variable in class in heap and usage of smart pointer
这里如何使用智能指针?
#include <iostream>
using namespace std;
struct node
{
char data;
node *next;
};
class linked_list
{
private:
node *head,*tail,*tmp;
public:
linked_list()
{
head = nullptr;
tail = nullptr;
}
void add_node()
{
tmp = new node();
cin>> tmp->data;
tmp->next = nullptr;
if(head == nullptr)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
void print()
{
tmp=head;
while(tmp!=nullptr)
{
cout<<tmp->data;
tmp=tmp->next;
cout<<endl;
}
}
};
int main()
{
linked_list a;
a.add_node();
a.add_node();
a.add_node();
a.add_node();
a.print();
return 0;
}
如何更改 add_note
函数的第一行代码以使用智能指针,而不必担心以后的 new
/delete
。需要进行哪些更改?我应该更改多行代码吗?如果有,那么在哪里?
std::unique_ptr
可能是最合适的:
#include <iostream>
#include <memory>
struct node
{
char data;
std::unique_ptr<node> next;
};
class linked_list
{
private:
std::unique_ptr<node> head;
node* tail;
public:
linked_list()
: tail( nullptr )
{
}
void add_node()
{
std::unique_ptr<node> tmp(new node());
std::cin >> tmp->data;
if(head == nullptr)
{
head = std::move(tmp);
tail = head.get();
}
else
{
tail->next = std::move(tmp);
tail = tail->next.get();
}
}
void print()
{
auto tmp=head.get();
while(tmp!=nullptr)
{
std::cout << tmp->data;
tmp = tmp->next.get();
std::cout << "\n";
}
}
};
int main()
{
linked_list a;
a.add_node();
a.add_node();
a.add_node();
a.add_node();
a.print();
return 0;
}
一些注意事项:
- 我已经删除了
tmp
成员变量,没有充分的理由将它作为成员变量。
tail
是原始指针,因为节点是通过来自 head
成员 的链拥有的
- 您可以使用
shared_ptr
而不是 unique_ptr
,但单一所有权链似乎对链表更有意义
这里如何使用智能指针?
#include <iostream>
using namespace std;
struct node
{
char data;
node *next;
};
class linked_list
{
private:
node *head,*tail,*tmp;
public:
linked_list()
{
head = nullptr;
tail = nullptr;
}
void add_node()
{
tmp = new node();
cin>> tmp->data;
tmp->next = nullptr;
if(head == nullptr)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
void print()
{
tmp=head;
while(tmp!=nullptr)
{
cout<<tmp->data;
tmp=tmp->next;
cout<<endl;
}
}
};
int main()
{
linked_list a;
a.add_node();
a.add_node();
a.add_node();
a.add_node();
a.print();
return 0;
}
如何更改 add_note
函数的第一行代码以使用智能指针,而不必担心以后的 new
/delete
。需要进行哪些更改?我应该更改多行代码吗?如果有,那么在哪里?
std::unique_ptr
可能是最合适的:
#include <iostream>
#include <memory>
struct node
{
char data;
std::unique_ptr<node> next;
};
class linked_list
{
private:
std::unique_ptr<node> head;
node* tail;
public:
linked_list()
: tail( nullptr )
{
}
void add_node()
{
std::unique_ptr<node> tmp(new node());
std::cin >> tmp->data;
if(head == nullptr)
{
head = std::move(tmp);
tail = head.get();
}
else
{
tail->next = std::move(tmp);
tail = tail->next.get();
}
}
void print()
{
auto tmp=head.get();
while(tmp!=nullptr)
{
std::cout << tmp->data;
tmp = tmp->next.get();
std::cout << "\n";
}
}
};
int main()
{
linked_list a;
a.add_node();
a.add_node();
a.add_node();
a.add_node();
a.print();
return 0;
}
一些注意事项:
- 我已经删除了
tmp
成员变量,没有充分的理由将它作为成员变量。 tail
是原始指针,因为节点是通过来自head
成员 的链拥有的
- 您可以使用
shared_ptr
而不是unique_ptr
,但单一所有权链似乎对链表更有意义