/* seq.h - Matt Mahoney, mmahoney@cs.fit.edu ANALYSIS A Seq is a fixed numeric sequence. The declaration Seq s(n, start, step); defines a sequence of elements with fixed values s[0] = start s[1] = start + step s[2] = start + step*2 ... s[n-1] = start + step*(n-1) All constructor arguments default as follows: step = 1 start = 0 n = 0 s.size() returns n. s.begin() and s.end() return Seq::const_iterator, input iterators pointing to s[0] and s[n] respectively. Seq::iterator is a synonym for Seq::const_iterator. Indexing out of bounds throws an out_of_range exception. For example: Seq s(4, 10, 2); // {10, 12, 14, 16} cout << s[1]; // 12 for (Seq::iterator p=s.begin(); p!=s.end(); ++p) cout << " " << *p // 10 12 14 16 try { s[s.size()]; // Error: s[4] } catch (out_of_range x) { cerr << "Index out of bounds\n"; } DESIGN A Seq s is represented by a vector v, such that s[i] == v[i]. The elements are initialized by the constructor and remain fixed. The iterator types are both vector::const_iterator. */ #include #include template class Seq { private: std::vector v; // Precomputed elements public: Seq(int n=0, const T& start=0, const T& step=1): v(n) { for (int i=0; i=int(v.size())) throw std::out_of_range("Seq out of range"); else return v[i]; } typedef std::vector::const_iterator const_iterator; typedef std::vector::const_iterator iterator; iterator begin() const {return v.begin();} iterator end() const {return v.end();} };