获取 std::string 字符串的大小(以字节为单位)
我想获取 std::string 的字符串在内存中占用的字节数,而不是字符数。该字符串包含多字节字符串。 std::string::size()
会为我做这个吗?
编辑:另外,size()
还包括终止NULL
吗?
I would like to get the bytes a std::string
's string occupies in memory, not the number of characters. The string contains a multibyte string. Would std::string::size()
do this for me?
EDIT: Also, does size()
also include the terminating NULL
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
std::string
对字节进行操作,而不是对 Unicode 字符进行操作,因此std::string::size()
确实会返回以字节为单位的数据大小(不带当然,std::string 需要存储数据的开销)。不,
std::string
仅存储您告诉它存储的数据(它不需要尾随NULL
字符)。因此,它不会包含在大小中,除非您显式创建一个带有尾随 NULL 字符的字符串。std::string
operates on bytes, not on Unicode characters, sostd::string::size()
will indeed return the size of the data in bytes (without the overhead thatstd::string
needs to store the data, of course).No,
std::string
stores only the data you tell it to store (it does not need the trailingNULL
character). So it will not be included in the size, unless you explicitly create a string with a trailingNULL
character.您可能对此很迂腐:
但是
std::string::value_type
是char
并且sizeof(char)
定义为1
。仅当您
typedef
字符串类型时,这一点才变得重要(因为它可能在将来或由于编译器选项而改变)。You could be pedantic about it:
But
std::string::value_type
ischar
andsizeof(char)
is defined as1
.This only becomes important if you
typedef
the string type (because it may change in the future or because of compiler options).std::string::size()
确实是以字节为单位的大小。std::string::size()
is indeed the size in bytes.要获取字符串使用的内存量,您必须将
capacity()
与用于管理的开销相加。请注意,它是capacity()
而不是size()
。容量决定分配的字符数 (charT
),而size()
告诉您实际使用的字符数。特别是,
std::string
实现通常不会 *shrink_to_fit* 内容,因此,如果您创建一个字符串,然后从末尾删除元素,size()
将会递减,但在大多数情况下(这是实现定义的)capacity()
不会递减。某些实现可能不会分配所需的确切内存量,而是获取给定大小的块以减少内存碎片。在使用字符串的两倍大小的块的实现中,大小为
17
的字符串可以分配多达32
个字符。To get the amount of memory in use by the string you would have to sum the
capacity()
with the overhead used for management. Note that it iscapacity()
and notsize()
. The capacity determines the number of characters (charT
) allocated, whilesize()
tells you how many of them are actually in use.In particular,
std::string
implementations don't usually *shrink_to_fit* the contents, so if you create a string and then remove elements from the end, thesize()
will be decremented, but in most cases (this is implementation defined)capacity()
will not.Some implementations might not allocate the exact amount of memory required, but rather obtain blocks of given sizes to reduce memory fragmentation. In an implementation that used power of two sized blocks for the strings, a string with size
17
could be allocating as much as32
characters.是的,size() 将为您提供字符串中
char
的数量。多字节编码中的一个字符占用多个char
。Yes, size() will give you the number of
char
in the string. One character in multibyte encoding take up multiplechar
.所写问题存在固有冲突:
std::string
被定义为std::basic_string
——即它的元素类型是char
(1字节),但后来你说“该字符串包含多字节字符串”(“multibyte”==wchar_t
?)。size()
成员函数不计算尾随 null。它的值表示字符数(而不是字节数)。假设您打算说您的多字节字符串是
std::wstring
(std::basic_string
的别名),的内存占用量code>std::wstring
的字符,包括空终止符是:考虑如何编写一个可重用的模板函数,该函数适用于 std::basic_string<> 的任何潜在实例化,是有启发性的。像这样**:
** 为了简单起见,忽略很少为
std::basic_string<>
显式指定的特征和分配器类型(它们有默认值)。There is inherent conflict in the question as written:
std::string
is defined asstd::basic_string<char,...>
-- that is, its element type ischar
(1-byte), but later you stated "the string contains a multibyte string" ("multibyte" ==wchar_t
?).The
size()
member function does not count a trailing null. It's value represents the number of characters (not bytes).Assuming you intended to say your multibyte string is
std::wstring
(alias forstd::basic_string<wchar_t,...>
), the memory footprint for thestd::wstring
's characters, including the null-terminator is:It's instructive to consider how one would write a reusable template function that would work for ANY potential instantiation of std::basic_string<> like this**:
** For simplicity, ignores the traits and allocator types rarely specified explicitly for
std::basic_string<>
(they have defaults).