What C ++ list size () comes to mind, listsize

Source: Internet
Author: User

What C ++ list size () comes to mind, listsize

Valid STL focuses on listing in an Item. size () is an O (n) efficiency. At that time, I just wrote down the efficiency. Later I saw some people in csdn encountered a pitfall in the actual project. I 've been idle for nothing to take a good look at the STL list-related part:

Let's take a look at the reason why the STL experts set it to O (n:

1) size () calls distance () in algorithm to get the length, and empty () is to judge whether the header and tail are equal. O (1) In most cases, we all judge whether the list is empty.

2) design reason: list introduces an important splice operation. The constant level, that is, o (1) completes the list tranfer operation, which is of great help to merge reverse sort if:

If size () is not set, tranfer cannot be o (1), not o (1) therefore, Merge and sort cannot be completed efficiently. In fact, merge and sort operations are more important. Therefore, size () calls distance instead of better O (1) during each call) in this way, we can share the constant-level operations of tranfer to see people on the internet speak out. I want to understand the above design principles and will not talk about this structure.


What is "# define list_init_size 80" in C language?

A macro is defined. The macro name is list_init_size and its value is 80.
It is equivalent to a constant. As long as list_init_size is used in subsequent programs, the value of list_init_size is 80.

What is the difference between list = null and listsize () = 0?

Of course there are differences. For example, I have an empty water cup (list), but you don't have it. If you are null, my size is 0. If you want to hold water, you need to buy a water cup (new ArrayList ();), and I can directly hold water (list. add (water )). If you install it directly, the water will flow out (the NULL pointer is abnormal ). So list is often used for judgment! = Null & list. size ()! = 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.