在C ++中删除字符串对象 [英] Deleting string object in C++

查看:206
本文介绍了在C ++中删除字符串对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的C ++程序中有一个字符串对象声明如下:

I have a string object in my C++ program declared as follows:

string str;

我已经复制了一些数据,并做了一些操作。现在我想从内存中删除str对象。我不能使用删除运算符,因为str不是一个指针。如何从内存中删除该对象以回收分配给它的内存?

I have copied some data into it and done some operations. Now I want to delete the str object from the memory. I am not able to use delete operator since str is not a pointer. How do I delete that object from the memory to reclaim the memory allocated to it?

谢谢,
Rakesh。

Thanks, Rakesh.

推荐答案

你不必。当字符串超出范围时,它的析构函数将被自动调用,并且内存将被释放。

You don't have to. When the string goes out of scope, it's destructor will be called automatically and the memory will be freed.

如果你想立即清除字符串(无需等待,直到它超出范围)只需使用 str.clear()

If you want to clear the string right now (without waiting until it goes out of scope) just use str.clear().

这篇关于在C ++中删除字符串对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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