Start learning free

LESSON 102 OF 667 · ARRAYS I

Acessing vector elements in C++

Lesson

We can access the data inside a vector using indices and the bracket notation ([]), just like we do for static arrays.

Vectors also use zero-based indexing, so the first element in a vector has an index of 0.

vector<int> array = {50, 60, 70};

cout << array[0] << endl; // Output: 50

int data = array[1];
cout << data << endl; // Output: 60

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check