The hole in the size of the STL

Source: Internet
Author: User
Last year, just into the company, the company's basic library has a lrucache, the implementation of the inside is std::list. But the function of calculating std::list in the inside is directly using the size () method inside the STL. When the results are measured, the performance card is here.
Today, a colleague in the base library found in the implementation of a queue, also used in the std::list, take the number of elements when the same size () method.
Unfortunately, the company's base library exposes only the header files and cannot check to see if the pit will be available elsewhere.

Let's look at the implementation of the size () method in STL:

Size_type      size () const      {return std::d istance (Begin (), end ());}

The above code g++ version is 4.2.1

Obviously, the complexity is O (N). When the list contains more elements, the calculation will be slow. So, when using std::list, you must remember to maintain a count.

  • Related Article

    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.