April 2018
Intermediate to advanced
322 pages
6h 57m
English
Let's implement the Get() method. It simply returns the value of the selected index. However, we need to ensure that the index passed to the method is not out of bounds. The implementation should be as follows:
int List::Get(int index){ // Check if the index is out of bound if(index < 0 || index > m_count) return -1; return m_items[index];}
As we can see in the preceding code, the complexity of the Get() method is O(1) since it doesn't depend on the number of the List elements.