pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

list::size() 真的是 O(n) 嗎?

Is list::size() really O(n)?(list::size() 真的是 O(n) 嗎?)
本文介紹了list::size() 真的是 O(n) 嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

最近,我注意到有人提到 std::list::size() 具有線性復雜度.
根據 some sources,這實際上取決于實現,因為標準沒有說明復雜性是什么.
在此博客條目中的評論說:

Recently, I noticed some people mentioning that std::list::size() has a linear complexity.
According to some sources, this is in fact implementation dependent as the standard doesn't say what the complexity has to be.
The comment in this blog entry says:

其實要看你是哪個STL正在使用.微軟 Visual Studio V6將 size() 實現為 {return (_Size);而gcc(至少在版本中3.3.2 和 4.1.0) 這樣做 { return std::distance(begin(), end());} 這第一個有恒速,第二個有 o(N) 速度

Actually, it depends on which STL you are using. Microsoft Visual Studio V6 implements size() as {return (_Size); } whereas gcc (at least in versions 3.3.2 and 4.1.0) do it as { return std::distance(begin(), end()); } The first has constant speed, the second has o(N) speed

  1. 所以我的猜測是對于 VC++ 人群來說 size() 與 Dinkumware 一樣具有恒定的復雜性自 VC6 以來可能不會改變這一事實.我在嗎?
  2. 它目前在 gcc 中是什么樣子的?如果它真的是 O(n),為什么開發者選擇這樣做?
  1. So my guess is that for the VC++ crowd size() has constant complexity as Dinkumware probably won't have changed that fact since VC6. Am I right there?
  2. What does it look like currently in gcc? If it is really O(n), why did the developers choose to do so?

推薦答案

Pre-C++11 答案

您是正確的,標準沒有說明 list::size() 的復雜性必須是什么 - 但是,它確實建議它應該具有恒定的復雜性"(表中的注釋 A65).

Pre-C++11 answer

You are correct that the standard does not state what the complexity of list::size() must be - however, it does recommend that it "should have constant complexity" (Note A in Table 65).

這是 Howard Hinnant 的一篇有趣的文章,解釋了為什么有些人認為 list::size() 應該有 O(N) 復雜度(主要是因為他們相信 O(1) list::size() 使得 list::splice() 具有 O(N) 復雜度)以及為什么 O(1) list::size() 是一個好主意(在作者看來):

Here's an interesting article by Howard Hinnant that explains why some people think list::size() should have O(N) complexity (basically because they believe that O(1) list::size() makes list::splice() have O(N) complexity) and why an O(1) list::size() is be a good idea (in the author's opinion):

  • http://howardhinnant.github.io/On_list_size.html

我認為論文的主要觀點是:

I think the main points in the paper are:

  • 很少有維護內部計數的情況,所以 list::size() 可以是 O(1) 導致拼接操作變成線性
  • 可能還有更多情況,某人可能不知道可能發生的負面影響,因為他們調用了 O(N) size()(例如他的一個示例,其中 list::size() 在持有鎖時被調用).
  • 不是允許 size() 是 O(N),為了最少驚喜",標準應該要求任何實現 size() 的容器以 O(1) 的方式實現它.如果容器不能做到這一點,它根本不應該實現 size().在這種情況下,容器的用戶將知道 size() 不可用,如果他們仍然想要或需要獲取容器中元素的數量,他們仍然可以使用 container::distance( begin(), end()) 來獲取該值 - 但他們會完全意識到這是一個 O(N) 操作.
  • there are few situations where maintaining an internal count so list::size() can be O(1) causes the splice operation to become linear
  • there are probably many more situations where someone might be unaware of the negative effects that might happen because they call an O(N) size() (such as his one example where list::size() is called while holding a lock).
  • that instead of permitting size() be O(N), in the interest of 'least surprise', the standard should require any container that implements size() to implement it in an O(1) fashion. If a container cannot do this, it should not implement size() at all. In this case, the user of the container will be made aware that size() is unavailable, and if they still want or need to get the number of elements in the container they can still use container::distance( begin(), end()) to get that value - but they will be completely aware that it's an O(N) operation.

我想我傾向于同意他的大部分推理.但是,我不喜歡他提議的對 splice() 重載的添加.必須傳入必須等于 distance( first, last)n 才能獲得正確的行為,這似乎是難以診斷錯誤的秘訣.

I think I tend to agree with most of his reasoning. However, I do not like his proposed addition to the splice() overloads. Having to pass in an n that must be equal to distance( first, last) to get correct behavior seems like a recipe for hard to diagnose bugs.

我不確定應該或可以做什么,因為任何更改都會對現有代碼產生重大影響.但就目前而言,我認為現有代碼已經受到影響 - 對于本應明確定義的某些內容,從一種實現到另一種實現的行為可能會有相當大的不同.也許一個人關于將大小緩存"并標記為已知/未知的評論可能會很好用 - 你會得到 O(1) 行為的攤銷 - 你唯一一次得到 O(N) 行為是當列表被一些 splice() 操作修改時.這樣做的好處是它可以由今天的實現者完成,而無需更改標準(除非我遺漏了一些東西).

I'm not sure what should or could be done moving forward, as any change would have a significant impact on existing code. But as it stands, I think that existing code is already impacted - behavior might be rather significantly different from one implementation to another for something that should have been well-defined. Maybe onebyone's comment about having the size 'cached' and marked known/unknown might work well - you get amortized O(1) behavior - the only time you get O(N) behavior is when the list is modified by some splice() operations. The nice thing about this is that it can be done by implementors today without a change to the standard (unless I'm missing something).

據我所知,C++0x 在這方面沒有任何改變.

這篇關于list::size() 真的是 O(n) 嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 科普仪器菏泽市教育教学仪器总厂| 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 带锯机|木工带锯机圆木推台锯|跑车带锯机|河北茂业机械制造有限公司| | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | 东莞办公家具厂家直销-美鑫【免费3D效果图】全国办公桌/会议桌定制 | 联系我们老街华纳娱乐公司官网19989979996(客服) | 长沙发电机-湖南发电机-柴油发电机供应厂家-长沙明邦智能科技 | 合景一建-无尘车间设计施工_食品医药洁净车间工程装修总承包公司 | 大型工业风扇_工业大风扇_大吊扇_厂房车间降温-合昌大风扇 | 微信小程序定制,广州app公众号商城网站开发公司-广东锋火 | 高铝砖-高铝耐火球-高铝耐火砖生产厂家-价格【荣盛耐材】 | 橡胶膜片,夹布膜片,橡胶隔膜密封,泵阀设备密封膜片-衡水汉丰橡塑科技公司网站 | 有源电力滤波装置-电力有源滤波器-低压穿排电流互感器|安科瑞 | 德国EA可编程直流电源_电子负载,中国台湾固纬直流电源_交流电源-苏州展文电子科技有限公司 | 金联宇电缆|广东金联宇电缆厂家_广东金联宇电缆实业有限公司 | 引领中高档酒店加盟_含舍·美素酒店品牌官网 | 合肥网络推广_合肥SEO网站优化-安徽沃龙First | 拉曼光谱仪_便携式|激光|显微共焦拉曼光谱仪-北京卓立汉光仪器有限公司 | 国资灵活用工平台_全国灵活用工平台前十名-灵活用工结算小帮手 | 合肥办公室装修 - 合肥工装公司 - 天思装饰| 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 沈阳真空机_沈阳真空包装机_沈阳大米真空包装机-沈阳海鹞真空包装机械有限公司 | 液压升降平台_剪叉式液压/导轨式升降机_传菜机定做「宁波日腾升降机厂家」 | 低粘度纤维素|混凝土灌浆料|有机硅憎水粉|聚羧酸减水剂-南京斯泰宝 | SPC工作站-连杆综合检具-表盘气动量仪-内孔缺陷检测仪-杭州朗多检测仪器有限公司 | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 过跨车_过跨电瓶车_过跨转运车_横移电动平车_厂区转运车_无轨转运车 | 废水处理-废气处理-工业废水处理-工业废气处理工程-深圳丰绿环保废气处理公司 | 商标转让-购买商标专业|放心的商标交易网-蜀易标商标网 | 艺术涂料|木纹漆施工|稻草漆厂家|马来漆|石桦奴|水泥漆|选加河南天工涂料 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 集装箱展厅-住人集装箱住宿|建筑|房屋|集装箱售楼处-山东锐嘉科技工程有限公司 | 上海橡胶接头_弹簧减震器_金属软接头厂家-上海淞江集团 | 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 挤出机_橡胶挤出机_塑料挤出机_胶片冷却机-河北伟源橡塑设备有限公司 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 列管冷凝器,刮板蒸发器,外盘管反应釜厂家-无锡曼旺化工设备有限公司 | 深圳离婚律师咨询「在线免费」华荣深圳婚姻律师事务所专办离婚纠纷案件 | 电机保护器-电动机综合保护器-浙江开民| 大流量卧式砂磨机_强力分散机_双行星双动力混合机_同心双轴搅拌机-莱州市龙跃化工机械有限公司 | 江苏皓越真空设备有限公司 |