1// vector::operator[]
2#include <iostream>
3#include <vector>
4
5int main ()
6{
7 std::vector<int> myvector (10); // 10 zero-initialized elements
8
9 std::vector<int>::size_type sz = myvector.size();
10
11 // assign some values:
12 for (unsigned i=0; i<sz; i++) myvector[i]=i;
13
14 // reverse vector using operator[]:
15 for (unsigned i=0; i<sz/2; i++)
16 {
17 int temp;
18 temp = myvector[sz-1-i];
19 myvector[sz-1-i]=myvector[i];
20 myvector[i]=temp;
21 }
22
23 std::cout << "myvector contains:";
24 for (unsigned i=0; i<sz; i++)
25 std::cout << ' ' << myvector[i];
26 std::cout << '\n';
27
28 return 0;
29}
1std::vector<int> vecOfNums{ 1, 4, 5, 22, 33, 2, 11, 89, 49 };
2// Access element at index 3
3int & element = vecOfNums[3];