Opposite to that, O(n) means, that the runtime depends linear on the number of the elements of the container. So O(1) means that the runtime of an operation on a container is constant and is independent of the size of the container. O(i) stands for the complexity (runtime) of an operation. Let me add a few additional remarks to the table. Depending on your use-case, std::vector may be a 95% fit, because most of the time you have to add or remove elements to your std::vector. You see, we have five sequential containers in the standard template library. To get the full picture, here are the sequential containers of the STL. So, if you don't like proof by authority, let me talk about the second point. The first point of random access via the index operator is apparent. I already wrote in my last post C++ Core Guidelines: The Standard Library about the third point. the lowest space overhead (contiguous layout has zero per-element overhead, which is cache-friendly).the fastest default access pattern (begin-to-end or end-to-begin is prefetcher-friendly).the fastest general-purpose access (random access, including being vectorization-friendly).Std::array and std::vector offer the following advantages: Here is the reason for the rule of thumb from the guideline: SL.con.2: Prefer using STL vector by default, unless you have a reason to use a different container If you are busy, you can stop to read, if not, continue. Here is a rule of thumb: If you want to add elements to your container or remove elements from your container, use a std::vector if not, use a std::array. What? If you don't believe me, read this post. In 99 % of your use-cases for a sequential container, you are totally fine with a std::array or a std::vector.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |