std :: string是否需要将其字符存储在连续的内存中? [英] Does std::string need to store its character in a contiguous piece of memory?

查看:191
本文介绍了std :: string是否需要将其字符存储在连续的内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在C ++ 98中,不需要std::basic_string<>std::vector<>来使用连续存储.一经指出,这便被视为对std::vector<>的疏忽,并且,如果我没记错的话,它已由C ++ 03修复.

I know that in C++98, neither std::basic_string<> nor std::vector<> were required to use contiguous storage. This was seen as an oversight for std::vector<> as soon as it was pointed out, and, if I remember correctly, got fixed with C++03.

似乎记得曾经读过有关要求std::basic_string<>在C ++ 11仍称为C ++ 0x时使用连续存储的讨论,但我并没有紧跟讨论然后,仍然在工作中仅限于C ++ 03,所以我不确定它的结果.

I seem to remember having read about discussions requiring std::basic_string<> to use contiguous storage back when C++11 was still called C++0x, but I haven't followed the discussion closely back then, and am still restricted to C++03 at work, so I am not sure what became of it.

使用连续存储是否需要std::basic_string<>? (如果是,那么首先需要哪个版本的标准?)

So is std::basic_string<> required to use contiguous storage? (If so, then which version of the standard required it first?)

如果您想知道:如果您将代码将&str[0]的结果传递给希望写入连续内存的函数,则这很重要. (我知道str.data(),但是出于明显的原因,旧代码不使用它.)

In case you wonder: This is important if you have code passing the result of &str[0] to a function expecting a contiguous piece of memory to write to. (I know about str.data(), but for obvious reasons old code doesn't use it.)

推荐答案

The C++11 standard, basic_string 21.4.1.5,

应在basic_string对象中存储类似char的对象 连续地.也就是说,对于任何basic_string对象,标识 & *(s.begin()+ n)==& * s.begin()+ n对于n的所有值均成立 使得0< = n< s.size().

The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().

这篇关于std :: string是否需要将其字符存储在连续的内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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