是std :: string size()a O(1)操作吗? [英] Is std::string size() a O(1) operation?

查看:286
本文介绍了是std :: string size()a O(1)操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我使用的STL的实现是在VC ++中构建的

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

推荐答案

如果你问MSVC的string :: stize()的实现是否有复杂性,那么答案是肯定的。但是 Don Wakefield 提到了C ++标准23.1中的表65,说, size()的复杂性应该遵循'Note A'中的说法。注意A说:

If you're asking if MSVC's implementation of string::stize() 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:


标记为'(注释A)'的条目应该具有不变的复杂性。

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 it's not required.

注意A被添加到标准以安抚那些相信 size()应该允许具有线性复杂性,因此在修改容器时不需要维护大小。

'Note A' was added to the standard specifically to appease those who believed that size() should be allowed to have linear complexity so there would be no need to maintain the size when containers were modified.

所以你不能依赖 size()具有固定的复杂性,但我真的不知道if有任何实现没有一个常量 string :: size()

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().

这篇关于是std :: string size()a O(1)操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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