std :: string对visual studio的具体行为? [英] Specific behaviour of std::string on visual studio?

查看:424
本文介绍了std :: string对visual studio的具体行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我需要读/写大文件。



我决定使用ifstream :: read()文件在一个单一的传递,到一个std :: string的内存。
(这似乎是在c ++中做最快的方法: http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html
http://insanecoding.blogspot.com/2011/11/reading-in-entire- file-at-once-in-c.html



在文件之间切换时,我需要重置std :: string前一个内存缓冲区(即,擦除char []缓冲区以释放内存)



我试过:

  std :: string :: clear()
std :: string :: assign()
std :: string :: erase string :: npos)
std :: string :: resize(0)
std :: string :: reserve(0)
>

但是,在Visual Studio 2008下,这不会释放std :: string自身内部使用的内存:它的底层缓冲区不会被取消分配。



我发现删除它的唯一方法是调用std :: string :: swap(std :: string())
强制更改内部缓冲区



我发现这种行为有点奇怪...



我只在Visual Studio 2008上测试,我不知道它是一个STL标准的行为还是MSVC特有的。



你能给我一些线索?

解决方案

如Vlad和Alf评论, std :: string()。swap(the_string)是C ++ 98方式释放 the_string 的容量, the_string.shrink_to_fit()是C ++ 11的方式。



至于为什么 clear() erase() resize()等不这样做,这是一个优化,和结束。如果 clear()释放了字符串的容量,则通常必须在下一次迭代时重新分配类似的空间量,这将需要一些时间,容量周围。此实现不受标准保证,但在实现中非常常见。



reserve()


调用具有小于capacity()的res_arg参数的reserve()实际上是一个非绑定的收缩请求。具有res_arg< = size()的调用实际上是非绑定的缩小适配请求。


实现更有可能释放 reserve()调用的容量。如果我正在阅读它们, libc ++ 当你调用 reserve(0)时,libstdc ++会释放空间,但是VC ++的库已经做出了相反的选择。



编辑:正如penelope所说, std :: string 的行为在这里往往与 std :: vector 的行为。


I've got a project in which I need to read/write large files.

I've decided to use ifstream::read() to put those files into memory in one single pass, into an std::string. (that seems to be the fastest way to do it in c++ : http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html and http://insanecoding.blogspot.com/2011/11/reading-in-entire-file-at-once-in-c.html)

When switching between files, I then need to "reset" the std::string used as the previous memory buffer (ie, erase the char[] buffer to free memory)

I tried :

std::string::clear()
std::string::assign("")
std::string::erase(0, std::string::npos)
std::string::resize(0)
std::string::reserve(0)

but, under Visual Studio 2008, this doesn't free the memory used inside the std::string itself : its underlying buffer isn't de-allocated.

The only way I found to delete it is to call std::string::swap(std::string("")) to force changing the internal buffers between the actual std::string and the empty one in param.

I find this behaviour a bit strange...

I only tested on Visual Studio 2008, I don't know if it's a STL-standard behaviour or if it's MSVC-specific.

Could you get me some clue ?

解决方案

As Vlad and Alf commented, std::string().swap(the_string) is the C++98 way to release the_string's capacity, and the_string.shrink_to_fit() is the C++11 way.

As to why clear(), erase(), resize(), etc. don't do it, this is an optimization to reduce allocations when you use a string over and over. If clear() freed the string's capacity, you'd generally have to reallocate a similar amount of space on the next iteration, which would take some time the implementation can save by keeping the capacity around. This implementation isn't guaranteed by the standard, but it's very common in implementations.

reserve() is documented with

Calling reserve() with a res_arg argument less than capacity() is in effect a non-binding shrink request. A call with res_arg <= size() is in effect a non-binding shrink-to-fit request.

which implies that implementations are more likely to release the capacity on a reserve() call. If I'm reading them right, libc++ and libstdc++ do release space when you call reserve(0), but it's plausible for VC++'s library to have made the opposite choice.

Edit: As penelope says, std::string's behavior here tends to be exactly the same as std::vector's behavior.

这篇关于std :: string对visual studio的具体行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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