为什么std :: strstream不推荐使用? [英] Why was std::strstream deprecated?

查看:356
本文介绍了为什么std :: strstream不推荐使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现, std :: strstream 已被弃用,赞成 std :: stringstream 。这是一段时间,因为我使用它,但它做了我需要做的,所以惊讶地听到它的贬低。

I recently discovered that std::strstream has been deprecated in favor of std::stringstream. It's been a while since I've used it, but it did what I needed to do at the time, so was surprise to hear of its deprecation.

我的问题是为什么这个决定,以及 std :: stringstream 提供的 std :: strstream

My question is why was this decision made, and what benefits does std::stringstream provide that are absent from std::strstream?

推荐答案

strstream c $ c> char * 这很难管理,因为它没有说明它是如何分配的。因此,不可能知道你是否应该删除它或调用free()或完全做其他事情。关于唯一真正令人满意的解决方法是通过 freeze()函数将它返回到 strstream 这是非常明显的,很多人错了。 stringstream 返回一个管理自身的字符串对象,这样容易出错。

The strstream returned a char * that was very difficult to manage, as nowhere was it stated how it had been allocated. It was thus impossible to know if you should delete it or call free() on it or do something else entirely. About the only really satisfactory way to deallocate it was to hand it back to the strstream via the freeze() function. This was sufficiently non-obvious, that lots of people got it wrong. The stringstream returns a string object which manages itself, which is far less error prone.

必须使用结束来终止字符串,但我相信解除分配问题是弃用的主要原因。

There was also the issue of having to use ends to terminate the string, but I believe the deallocation problem was the main reason for deprecation.

这篇关于为什么std :: strstream不推荐使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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