std::string size() 是 O(1) 操作吗?

发布于 2024-07-08 18:21:25 字数 66 浏览 6 评论 0原文

std::string size() 是 O(1) 操作吗?

我使用的STL实现是VC++中内置的

Is std::string size() a O(1) operation?

The implementation of STL I'm using is the one built into VC++

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

淡淡的优雅 2024-07-15 18:21:25

如果您问 MSVC 的 string::size() 实现是否具有恒定的复杂性,那么答案是肯定的。 但是 Don Wakefield 提到了 C++ 标准 23.1 中的表 65,其中表示 size() 的复杂性应遵循“注释 A”中所述。 注A说:

那些标记为“(注释 A)”的条目
应该具有恒定的复杂性。

然而,这并不意味着这些条目应具有恒定的复杂性。 标准使用非常具体的术语,“应该”意味着它不是强制性的。

标准中添加“注释 A”专门是为了安抚那些认为应该允许 size() 具有线性复杂度的人,这样在修改容器时就不必保留大小了。

因此,您不能依赖 size() 具有恒定的复杂性,但老实说,我不确定是否有任何实现没有恒定的 string::size().

If you're asking if MSVC's implementation of string::size() has constant complexity, then the answer is yes. But Don Wakefield mentioned Table 65 in 23.1 of the C++ Standard where it says that the complexity of size() should follow what's said in 'Note A'. Note A says:

Those entries marked ‘‘(Note A)’’
should have constant complexity.

However, that does not mean that those entries shall have constant complexity. Standards use very specific terminology, and "should" means that it is not mandatory.

'Note A' was added to the standard specifically to appease those who believed that size() should be allowed to have linear complexity so it would not be necessary to keep the size when the containers were modified.

So you can't rely on size() having constant complexity, but I'm honestly not sure if there are any implementations that do not have a constant string::size().

嗼ふ静 2024-07-15 18:21:25

是的,std::string::size() 的复杂度是 O(1)。

Yes, std::string::size() is O(1).

我做我的改变 2024-07-15 18:21:25

这是回答 msvc++ 这个问题的简单方法。

在项目中编写一些代码:

string happy;
happy.size();

突出显示 .size 调用,右键单击,转到定义。

在我的安装(vs2005sp1)中,这会将我发送到 xstring:1635,它看起来像这样:

size_type __CLR_OR_THIS_CALL size() const
    {   // return length of sequence
    return (_Mysize);
    }

所以看起来该字符串有一个名为 _Mysize 的成员,它只是返回该成员。

换句话说,这是一个 O(1) 的实现。

Here's an easy way to answer that question for msvc++.

Write some code in a project:

string happy;
happy.size();

Hilight the .size call, right-click, go to definition.

On my install (vs2005sp1) this sends me to xstring:1635, which looks like this:

size_type __CLR_OR_THIS_CALL size() const
    {   // return length of sequence
    return (_Mysize);
    }

So it looks like the string has a member called _Mysize, and it's just returning that.

In other words, this is a O(1) implementation.

待"谢繁草 2024-07-15 18:21:25

参见标准第 23.1 节中的表 65。 “a.size()”被列为“(注释A)”,它表示“那些条目......应该具有恒定的复杂性”。

第21.3节说字符串符合序列(23.1)的要求,事实上,size()是常数时间。

See Table 65 in Section 23.1 of the Standard. "a.size()" is listed as "(Note A)", which says that "Those entries ... should have constant complexity".

Section 21.3 says that strings conform to the requirements of a sequence (23.1), ipso facto, size() is constant time.

岁月流歌 2024-07-15 18:21:25

对于字符串,对于所有不使用绳索的字符串实现,size() 操作必须保持不变(1) 。 标准中没有明确要求操作时间为 O(1),最接近的是 size() 应该 时间恒定,但这为任何其他复杂性度量留出了空间。

那么为什么必须它是O(1)?

这是因为无法根据字符串本身的内容计算大小。 在 C 中,您使用 NUL 终止符来确定字符串的结尾,而在 C++ 中,NUL 与字符串中的任何其他字符一样有效。 由于字符串的大小无法根据内容(2)计算出来,因此必须在外部进行处理,而与字符串的实际大小无关。

(1) C++03 标准允许实现使用ropes 作为字符串的实现,但事实是标准库的当前实现都没有使用它们。

(2) 如果实现使用绳索,则操作可以通过构造绳索的块的数量来取决于大小(如果块是通过链表或类似构造链接的),或者如果允许它们有不同的尺寸。 但据我所知,绳索并未在任何标准库实现中使用。

For a string, the size() operation has to be constant for all string implementations that don't use ropes(1). There is no explicit requirement in the standard that requires the operation to be O(1), the closest is the generic requirement that size() should be constant time, but that leaves room for any other complexity measure.

So why must it be O(1)?

This comes from the fact that the size cannot be calculated from the contents of the string itself. While in C you use a NUL terminator to determine the end of the string, in C++ NUL is as valid as any other character in the string. Since the size of the string cannot be calculated from the contents(2), it must be handled externally, independently of the actual size of the string.

(1) C++03 standard allows an implementation to use ropes as the implementation for strings, but the fact is that none of the current implementations of the standard libraries use them.

(2) If the implementation used ropes, the operation could depend on the size by means of the number of blocks from which the rope was constructed if the blocks were linked through a linked list or similar construct, or if they were allowed to have different sizes. But ropes are not used in any standard library implementation that I know of.

甜中书 2024-07-15 18:21:25

STL 保证容器的性能至少为 O(N),但是包括 std::string 在内的许多容器都可以将其实现为 O(1),并且将会这样做。 通常它会返回一个简单的变量或执行类似 _End - _Begin 的操作并返回该变量。

Performance is guaranteed by the STL to be at least O(N) for containers, however many containers including std::string can implement this as O(1) and will. Usually it'll either return a simple variable or do something like _End - _Begin and return that.

小…红帽 2024-07-15 18:21:25

size() 的复杂度应该遵循“注释A”。 这意味着,它应该具有恒定的复杂度,即 O(1)。 虽然我不确定具体的实现,但 C++ 中的迭代器确实具有指向 STL 容器的关联操作,例如 begin() 和 end()。 这些操作具有恒定的时间复杂度,因为它们是容器的要求。 这意味着 begin() - end() 也具有恒定的复杂性。 这意味着 size() 是 O(1) 操作。

The complexity of size() should follow 'Note A'. That means, it should have the constant complexity, ie O(1). Although, I am not sure of the implementation, but the iterators in C++ do have associated operations like begin() and end() that point to the STL containers. These operations have a constant time complexity as they are the container requirements. This implies that begin() - end() also has constant complexity. That means, size() is O(1) operation.

┼── 2024-07-15 18:21:25
size_type __CLR_OR_THIS_CALL size() const

{   // return length of sequence

    return (_Mysize);

}

所以最终可能会是这样,但你永远无法确定。

size_type __CLR_OR_THIS_CALL size() const

{   // return length of sequence

    return (_Mysize);

}

So it eventually might be like this, but you can never be sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文