//* Introduction to the concept of iterator (pointer alike data structure) *

#include <cstddef> #include<iostream> #include<vector> using namespace std; int main() { vector<double> V; size_t n; cout << "Please enter the number of elements for the vector" << endl; cin >> n; V.resize(n); cout << "Please enter the values for the vector one by one:" << endl; size_t i; for (i=0; i<V.size(); i++) cin >> V[i]; // classic array style access for (i=0; i<V.size(); i++) cout << " " << V[i]; cout << endl; // sequential pointer-alike access with iterators vector<double>::iterator it; for (it=V.begin(); it!=V.end(); ++it) cout << " " << *it; cout << endl; // V.begin() - inline FN that returns memory location of the first vector element // *V.begin() is equivalent to V.front() // V.end() - inline FN that returns memory location of one AFTER the last used vector element // *V.end() IS NOT equivalent to V.back(), in fact *V.end() must not be used at all // what about constant arrays? (this one was not constant) vector<double>::const_iterator cit; for (cit=V.begin(); cit!=V.end(); ++cit) cout << " " << *cit; cout << endl; // reverse? - what's that? vector<double>::reverse_iterator rit; for (rit=V.rbegin(); rit!=V.rend(); ++rit) cout << " " << *rit; cout << endl; // const_reverse_? - oh, yes! It does exist // ... left for your own experimentation ... return(0); }