#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> num {1, 2, 3, 4, 5};
// declare iterator
vector<int>::iterator iter;
// initialize the iterator with the first element
iter = num.begin();
// print the vector element
cout << "num[0] = " << *iter << endl;
// iterator points to the 4th element
iter = num.begin() + 3;
cout << "num[3] = " << *iter << endl;
// iterator points to the last element
iter = num.end() - 1;
cout << "num[4] = " << *iter;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> num {1, 2, 3, 4, 5};
// declare iterator
vector<int>::iterator iter;
// use iterator with for loop
for (iter = num.begin(); iter != num.end(); ++iter) {
cout << *iter << " ";
}
return 0;
}
vector<int> num = {1, 2, 3, 4, 5};
vector<int>::iterator iter;
// iter points to num[0]
iter = num.begin();