C + + list size () things to think about

Source: Internet
Author: User

effective STL An item focuses on the List.size () is an O (n) efficiency at that time just to note down the back look at the csdn someone in the actual project encountered a pit I recently idle to have nothing to do with the STL list related part of the good look:

Look at the reason why the STL Daniel is set to O (n):

1) size () call algorithm in distance () to get the length and empty () is to determine whether the head and tail are equal O (1) Most of the time we are judging whether the list is empty

2) Design reason: List introduced an important splice operation, at the constant level that is O (1) To complete the list of Tranfer this operation on the merge reverse sort has a great help if:

Size () do not do so tranfer cannot be O (1) completion is not O (1) then the merge and sort cannot be done efficiently and the merge and sort operations are actually more important, so size () is called on each call distance And not better O (1) so that you can divide Tranfer's constant-level operation to see the online people spit groove This place I want to understand the above design principle will not spit groove this structure.

C + + list size () things to think about

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.