`
xuning2516
  • 浏览: 8092 次
  • 性别: Icon_minigender_1
  • 来自: 江西
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

C++ std::list.size() has linear complexity

 
阅读更多
size_type size() const {
    size_type __result = 0;
    distance(begin(), end(), __result);

    return __result;
  }

在sgi stl的实现版本中看到关于size member的实现。

这里的distance是一个全局函数。
对于random_access iterator 而言是两个迭代器相减,具有O(1)的复杂度,而对于其他类型的迭代器则是++first到last的形式,具有O(n)的复杂度。

Why islist<>::size()linear time?
Thesize()member function, forlistandslist, takes time proportional to the number of elements in the list. This was a deliberate tradeoff. The only way to get a constant-timesize()for linked lists would be to maintain an extra member variable containing the list's size. This would require taking extra time to update that variable (it would makesplice()a linear time operation, for example), and it would also make the list larger. Many list algorithms don't require that extra word (algorithms that do require it might do better with vectors than with lists), and, when it is necessary to maintain an explicit size count, it's something that users can do themselves.

This choice is permitted by the C++ standard. The standard says thatsize()"should" be constant time, and "should" does not mean the same thing as "shall". This is the officially recommended ISO wording for saying that an implementation is supposed to do something unless there is a good reason not to.

One implication of linear timesize(): you should never write

if (L.size() == 0) ...
Instead, you should write
if (L.empty()) ...
在VC下面是保存了一个size_t的静态变量来保存list中的size,但是这样做就需要在插入删除的操作中进行这个变量的维护,在例如splice的操作中复杂度将达到O(n)。这样对于list中插入和删除都是const complexity的特性就不符合了。
分享到:
评论

相关推荐

    C++标准库(第二版)英文版.pdf

    The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...

    C++四书五经 - 01. TCPL和D&E

    18 C++ Network Programming, Volume 1: Mastering Complexity with ACE and Patterns 19 C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks 7. 杂项 20 Thinking in C++, Volume 1: ...

    Computational.Complexity.A.Modern.Approach

    ### 计算复杂度:现代方法 #### 一、引言与基础知识 《计算复杂度:现代方法》是一本面向研究生级别的教科书,旨在介绍计算复杂度理论的经典成果及最新进展。本书不仅适合计算机科学领域的学生和研究人员,也适用...

    通信系统:动态规划实践.pptx

    However, with the increasing complexity of communication systems, optimizing their performance has become a daunting task. This is where dynamic programming comes into play. 动态规划(Dynamic ...

    C++四书五经 - 05. 标准库

    18 C++ Network Programming, Volume 1: Mastering Complexity with ACE and Patterns 19 C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks 7. 杂项 20 Thinking in C++, Volume 1: ...

    C++四书五经 - 07. 杂项-02

    18 C++ Network Programming, Volume 1: Mastering Complexity with ACE and Patterns 19 C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks 7. 杂项 20 Thinking in C++, Volume 1: ...

    C++四书五经 - 03. 高效健壮编程

    18 C++ Network Programming, Volume 1: Mastering Complexity with ACE and Patterns 19 C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks 7. 杂项 20 Thinking in C++, Volume 1: ...

    C++四书五经 - 07. 杂项-01

    18 C++ Network Programming, Volume 1: Mastering Complexity with ACE and Patterns 19 C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks 7. 杂项 20 Thinking in C++, Volume 1: ...

    C++四书五经 - 04. 模板和泛型编程

    18 C++ Network Programming, Volume 1: Mastering Complexity with ACE and Patterns 19 C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks 7. 杂项 20 Thinking in C++, Volume 1: ...

    海上无线通信技术:现状与挑战.pdf

    此外, future sea-based wireless communication technology faces many challenges, such as the complexity of the marine environment, the limitations of transmission distance, and the diversity of ...

    操作系统英文课件:ch4 File Systems.ppt

    文件系统的主要功能包括:present logical view of files and directories,hide complexity of hardware devices,facilitate efficient use of storage devices,optimize access to disk,support sharing and ...

    计算机组成与结构体系英文课件:Chapter3 BasicInputOutput.pdf

    It also highlights the importance of I/O device interfaces in managing the complexity of connecting different peripherals to the system. Understanding these fundamentals is vital for designing ...

    deep q_learning

    Training times can be very long depending on the complexity of the environment. [This repo](https://github.com/matthiasplappert/keras-rl-weights) provides some weights that were obtained by running ...

    MPEG-2标准第五部分:软件代码实现.rar

    5. **mpeg2aac**:这是对AAC编码的进一步细化,可能包括更高级的特性如多通道音频、AAC-LC(Low Complexity)、SBR(Spectral Band Replication)和PS(Parametric Stereo)等。这些技术增强了音频编码的效率和音质...

    大数据时代的人力资源管理:挑战与创新.pdf

    首先,大数据的核心特征包括规模性(Volume)、多样性(Variety)、高速性(Velocity),有时还包括可变性(Variability)、复杂性(Complexity)、低价值密度(Value)和真实性(Veracity)。这些特征决定了大数据...

    复杂性理论:一种现代方法Complexity Theory: A Modern Approach

    关于计算复杂性理论的教科书草案。 涵盖基本的复杂性类,具体计算模型的下限和一些高级主题。

    中国计算机学会推荐国际学术刊物与会议 计算机科学理

    JCOMPLEXITY (Journal of Complexity)** - **简介**:《复杂性杂志》专注于复杂性理论的研究。 - **研究范围**:包括计算复杂性、数学复杂性、信息复杂性等。 - **出版社**:Elsevier - **网址**:...

    A Low Complexity Algorithm for Generating Turbo.pdf

    ### 低复杂度算法生成Turbo码随机交织器 #### 摘要 本文提出了一种基于冒泡排序方法的低复杂度算法,该算法可用于生成满足多种标准(如s-随机性、码匹配准则以及Turbo Trellis Coded Modulation中的奇偶属性)的...

    Addison.Wesley.-.Imperfect.C++.Practical.Solutions.for.Real-Life.Programming.chm

    He shows you how to tame C++'s complexity, cut through its vast array of paradigms, take back control over your code--and get far better results. If you're a long-time C++ developer, this book will ...

    Programming.Scala_Tackle.Multi-Core.Complexity.on.the.Java.Virtual.Machine[2009][EN][PDF]

    Programming.Scala_Tackle.Multi-Core.Complexity.on.the.Java.Virtual.Machine[2009][EN][PDF] Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine by Venkat Subramaniam Scala is ...

Global site tag (gtag.js) - Google Analytics