To summarize the very good answers here:
- C ++ 11:
O(1) (@Jeffrey) - C ++ 98: unenforced, experiments need to be done based on container class
- C ++ 98 with default container: default container for std :: queue std :: deque, which calculates the size by subtracting two iterators, not
O(1) , but not less than O(C) . ( @juanchopanza )
Thus, if you need to provide O (1) -ness size () in C ++ 98, you have to keep your own score.
If I could, I would like to step on my soap box and thank the C ++ 11 group for closing this awful specification. Many languages โโ/ libraries (e.g. Scala) put a lot of effort into defining a BIG-O statement. Given that performance is the primary use case for C ++, I find this lack of specs amazing. It is completely unacceptable that you need to check the header code to determine the performance characteristics of the std classes.
source share