Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the time complexities for size?

I am studying the complexity of various operations of the different STL containers. Through a different question on this site I have found this chart.

website link

enter image description here

One operation I noticed was missing form this chart was the size operation. I would suppose that if one knew the complexity of .begin and .end one could also calculate the complexity for size. But those are also missing.

I have found an answer similar to what I am looking for in this question, but this one is for Java so it does not cover all the STL containers and it only defines the big O of size for a few of the given datatypes.

Does anyone know the complexity for the .size operation of the various containers or could someone give me a pointer as to where I could find these complexities. Any help would be greatly appreciated.

Also, if my question is wrongly phrased and/or off-topic. Do not hesitate to suggest an edit.

like image 767
Poseidaan Avatar asked Apr 23 '20 15:04

Poseidaan


People also ask

What is the time complexity of size () function?

size( ): It returns the number of elements in the list. Its time complexity is O(1).

What is the largest time complexity?

Linear time is the best possible time complexity in situations where the algorithm has to sequentially read its entire input.

What are time complexities of these?

Time complexity is defined as the amount of time taken by an algorithm to run, as a function of the length of the input. It measures the time taken to execute each statement of code in an algorithm.


1 Answers

Since C++11, the complexity of the size member function is constant for all standard containers.

std::forward_list which is an implementation of the singly linked list data structure does not provide a size member function. The size can be calculated in linear time using the iterators.

Aside from standard C++ containers, all data structures can be augmented with a separately stored size variable to achieve such constant complexity at the cost of small constant overhead on insert and delete operations. Array is special in regard that it does not require any additional overhead assuming iterator to end is stored.

like image 91
eerorika Avatar answered Nov 20 '22 06:11

eerorika