Junior — Middle
What is the time complexity of accessing an element by index in a vector container?
sobes.tech AI
Answer from AI
Access to an element by index in a std::vector container is performed in constant time — O(1). This is possible because vector stores elements in a contiguous block of memory, allowing direct access to an element by offset without traversing the structure.
Example:
std::vector<int> v = {10, 20, 30};
int x = v[1]; // access to the element in O(1) time