使用struct和class。 [英] using struct and class.

查看:97
本文介绍了使用struct和class。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。



有人可以向我解释为什么我要销毁名单。



hi.

can someone explain to me why do I have to destroy the list.

struct Node{
int count;
string word;
Node *next;

class LinkedList
Node* head
-
public:
addWord(string)





我们的任务是在这个程序中使用LinkedList。



程序的规格是读取文本文件

(文本文件包含一个句子)



在句子中我们必须计算一个字符出现在句子中的次数。显示最多出现的前5个字母。



我们的教授说我们还要算上这些字。也许是我们必须使用结构的原因。


他还提到要销毁这个名单?是使用析构函数吗?



we were tasked to used LinkedList in this program.

the specs of the program is to read a text file
(the text file contains a sentence)

in the sentence we have to count how many times a character appears in the sentence. display the top 5 letters that appears the most.

and our prof said that we have to count also the words. maybe the reason why we have to used a struct.

he also mentioned to destroy the list? is it with the use of destructor?

推荐答案

当你创建一个包含已分配内存的类或结构(如链表)时,你的责任就在于你的类在不再需要类的实例时释放此类内存。这是在您的结构的析构函数中完成的。



如果不这样做,那么每次使用结构时都会丢失内存,导致内存泄漏这意味着你的应用程序慢慢消耗所有可用内存并且系统崩溃。
When you create a class or struct that contains allocated memory (such as a linked list) it is the responsibility of you class to deallocate such memory when the instance of your class is no longer needed. This is done in the destructor for your structure.

If you don't, then each time you use your structure you lose memory, and that causes "memory leaks" which mean that your application slowly consumes all available memory and the system crashes.


你真正处于编码的起点。需要析构函数(简称dtor)来释放分配的内存。如果使用new []则必须删除[]。您必须删除每个结构,因此删除从列表的结尾(!!!!)开始。你最好阅读链接列表



问你的教授为什么计数器不在类中但在struct中。
You are really at a starting point of coding. A destructor (short "dtor") is needed to release memory allocated. If using new[] than you must delete[]. You must delete every struct, so deleting starts at the end (!!!!) of the list. Best you read about Linked list.

Ask your prof why the counter isnt in the class but in struct.


这篇关于使用struct和class。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆