• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            stl-什么時候用那種container?

            Overview of Container Abilities
              Vector Deque List Set Multiset Map Multimap
            Typical internal data structure Dynamic array Array of arrays Doubly linked list Binary tree Binary tree Binary tree Binary tree
            Elements Value Value Value Value Value Key/value pair Key/value pair
            Duplicates allowed Yes Yes Yes No Yes Not for the key Yes
            Random access available Yes Yes No No No With key No
            Iterator category Random access Random access Bidirectional Bidirectional (element constant) Bidirectional (element constant) Bidirectional (key constant) Bidirectional (key constant)
            Search/find elements Slow Slow Very slow Fast Fast Fast for key Fast for key
            Inserting/removing of elements is fast At the end At the beginning and the end Anywhere
            Inserting/removing invalidates iterators, references, pointers On reallocation Always Never Never Never Never Never
            Frees memory for removed elements Never Sometimes Always Always Always Always Always
            Allows memory reservation Yes No
            Transaction safe (success or no effect) Push/pop at the end Push/pop at the beginning and the end All except sort() and assignments All except multiple-element insertions All except multiple-element insertions All except multiple-element insertions All except multiple-element insertions


            As a supplement to the table, the following rules of thumb might help:

            • By default, you should use a vector. It has the simplest internal data structure and provides random access. Thus, data access is convenient and flexible, and data processing is often fast enough.

            • If you insert and/or remove elements often at the beginning and the end of a sequence, you should use a deque. You should also use a deque if it is important that the amount of internal memory used by the container shrinks when elements are removed. Also, because a vector usually uses one block of memory for its elements, a deque might be able to contain more elements because it uses several blocks.

            • If you insert, remove, and move elements often in the middle of a container, consider using a list. Lists provide special member functions to move elements from one container to another in constant time. Note, however, that because a list provides no random access, you might suffer significant performance penalties on access to elements inside the list if you only have the beginning of the list.

              Like all node-based containers, a list doesn't invalidate iterators that refer to elements, as long as those elements are part of the container. Vectors invalidate all of their iterators, pointers, and references whenever they exceed their capacity, and part of their iterators, pointers, and references on insertions and deletions. Deques invalidate iterators, pointers, and references when they change their size, respectively.

            • If you need a container that handles exceptions in a way that each operation either succeeds or has no effect, you should use either a list (without calling assignment operations and sort() and, if comparing the elements may throw, without calling merge (), remove(), remove_if(), and unique(); see page 172) or an associative container (without calling the multiple-element insert operations and, if copying/assigning the comparison criterion may throw, without calling swap()). See Section 5.11.2, for a general discussion of exception handling in the STL and Section 6.10.10, for a table of all container operations with special guarantees in face of exceptions.

            • If you often need to search for elements according to a certain criterion, use a set or a multiset that sorts elements according to this sorting criterion. Keep in mind that the logarithmic complexity involved in sorting 1,000 elements is in principle ten times better than that with linear complexity. In this case, the typical advantages of binary trees apply.

              A hash table commonly provides five to ten times faster lookup than a binary tree. So if a hash container is available, you might consider using it even though hash tables are not standardized. However, hash containers have no ordering, so if you need to rely on element order they're no good. Because they are not part of the C++ standard library, you should have the source code to stay portable.

            • To process key/value pairs, use a map or a multimap (or the hash version, if available).

            • If you need an associative array, use a map.

            • If you need a dictionary, use a multimap.

            posted on 2005-12-14 09:02 夢在天涯 閱讀(1630) 評論(1)  編輯 收藏 引用 所屬分類: STL/Boost

            評論

            # re: stl-什么時候用那種container? 2005-12-14 11:34 力為

            Effective STL Item One is also helpful.  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804154
            • 排名 - 5

            最新評論

            閱讀排行榜

            精品精品国产自在久久高清| 久久精品国产99国产精品澳门| 精品人妻伦九区久久AAA片69| 亚洲午夜久久影院| 久久毛片免费看一区二区三区| 亚洲AⅤ优女AV综合久久久| 中文字幕乱码人妻无码久久| 国产精品99久久久久久人| 久久久99精品成人片中文字幕| 亚洲AV无码久久精品蜜桃| 国产成人精品久久一区二区三区| 欧美久久综合九色综合| 亚洲国产精品无码久久久不卡| 久久综合久久久| 久久中文字幕人妻丝袜| 91精品国产91久久综合| 午夜精品久久久久久影视777| 天堂久久天堂AV色综合| 无码精品久久一区二区三区| 久久―日本道色综合久久| 久久中文字幕人妻熟av女| 久久久久亚洲精品无码网址| 久久久久久久久无码精品亚洲日韩 | 国产V亚洲V天堂无码久久久| 日韩十八禁一区二区久久| 777米奇久久最新地址| 奇米影视7777久久精品人人爽| 久久久久亚洲AV无码专区桃色 | 久久成人国产精品免费软件| 国产ww久久久久久久久久| 久久国产精品成人片免费| 久久精品极品盛宴观看| 久久人人爽人人爽人人片AV东京热| 大伊人青草狠狠久久| 国产精品久久久久久| 国产∨亚洲V天堂无码久久久| 亚洲中文字幕无码久久2020 | 久久久一本精品99久久精品66| 国产毛片欧美毛片久久久| 无码任你躁久久久久久老妇App| 久久亚洲国产成人影院网站|