尽管字符串具有空终止字符,但是std :: string的长度是否一致?(Is length of std::string consistent despite string having null

编程入门 行业动态 更新时间:2024-10-28 04:23:25
尽管字符串具有空终止字符,但是std :: string的长度是否一致?(Is length of std::string consistent despite string having null terminated characters?)

我的问题来自于在libstdc ++的basic_string.h中读取函数length()和size()的注释,该注释随Ubuntu 14.04一起发布:

评论声明如下:

返回字符串中的字符数,不包括任何空终止。

这个评论对我来说是混乱的,在这种情况下,字符串只包含空终止字符,特别是在考虑传统上strlen如何工作时,我担心编译器可能会以不返回size()但长度的方式实现字符串由char_traits :: length()确定。

虽然可以明显知道字符串长度而不需要空终止字符,但传统字符串长度的概念与容器的长度概念不同。 我担心这可能会在某些时候发生变化,我想知道代码的风险如下面的代码......

std::string header(4, char{}); assert(header.length() == 4);

......不是完全跨平台的。

我假设它(问题)必须保持有效,我的假设来自这个参考 :

My question arises from reading a comment of function length() and size() in basic_string.h of libstdc++, that is distributed with Ubuntu 14.04:

The comment states the following:

Returns the number of characters in the string, not including any null-termination.

This comment to me is confusion in the case where strings only contains null terminating characters, especially when considering how strlen works traditionally, and I am concerned that compilers might implement strings in a way where they don't return the size() but the length as determined by char_traits::length().

Whilst it might obvious that a string length is known without requiring null terminating characters, the notion of the length of a traditional string is not the same as that of a container. I feared that this might have changed at some point, and I'm wondering at the risk of code like the code below ...

std::string header(4, char{}); assert(header.length() == 4);

... not being entirely cross platform.

I'm assuming it (the question) must hold valid, and my assumption is from this reference:

最满意答案

其中字符串只包含空终止字符

那些不是“空终止字符”,因为它们不终止字符串,它们是它的一部分。 它们是空字符,但不是null终止。

您看到的行为由标准保证。 你告诉string创建一个由四个空字符组成的字符串,这就是你得到的。

在编译器之间或标准版本之间不可移植将是一件荒谬的事情。 它始终得到保证。

我的问题也出现了,因为这与strlen的传统行为不一致,而char_traits :: length()

这是无关紧要的,因为char*不知道它的长度。 一个std::string 。

我将更改libstdc ++标题中的注释,使您更加清楚。

where strings only contains null terminating characters

Those are not "null terminated characters", since they don't terminate the string, they are part of it. They are null characters, but not null terminated.

The behaviour you see is guaranteed by the standard. You told the string to create a string of four null characters, so that's what you get.

That would be a ridiculous thing to be non-portable between compilers, or between revisions of the standard. It's always been guaranteed.

My question also arises because this is inconsistent with the traditional behavior of strlen, and char_traits::length()

That's irrelevant, because a char* doesn't know its length. A std::string does.

I'll change the comment in the libstdc++ header that confused you to make it clearer.

更多推荐

本文发布于:2023-08-02 05:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1370061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   长度   字符   string   std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!