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.