4 ms·
Not all O(1) functions have the same cost. Suppose we have a container that stores its data on the heap, and has a null pointer and no heap object when it is em
by not2b 5y ago
Not all O(1) functions have the same cost. Suppose we have a container that stores its data on the heap, and has a null pointer and no heap object when it is empty. Then Container::empty() const just needs to check that pointer, where Container::size() const has to read the size out of the heap object, so c.size() == 0 has to cost more, as the compiler is unlikely to be able to optimize away the extra accesses.