#ifndef ARRAY_H #define ARRAY_H ///////////////////////////////////////////////////////////////////////// // Array.h - Template Array class // // // // Language: Visual C++, 2008 // // Platform: Dell Precision T7400, Vista Ultimate, SP1 // // Application: CSE382 - Algorithms and Data Structures, Fall 2008 // // Author: Jim Fawcett, Syracuse University, CST 4-187 // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////////// /* The Boost Library has a more complete Array class * http://www.boost.org/doc/libs/1_36_0/doc/html/boost/array.html * * Module Operations: * ================== * The Array class implements a template array that can be initialized, * has correct copy and assignment semantics, provides indexing and iteration, * and also provides a ToString() function for display of small arrays. * * Public Interface: * ================= * Array arr1 = { 1, 2, 3, 4, 5 }; * std::cout << arr1.ToString(); * Array arr2 = arr1; * arr2[0] = -5; * Array::iterator iter = arr1.begin(); * *(++iter) = -4; * arr2 = arr1; * size_t n = arr2.size(); * * Required Files: * =============== * Array.h, Array.cpp * * Build Command: * ============== * cl /EHa Array.cpp * * Maintenance History: * ==================== * ver 1 : 13 Sep 08 * - first release * * * Design Notes: * ============= * In order to use an initialization sequence, as shown above, Array * must be an aggregate type. To achieve that, the class must not have: * 1. user defined constructors * 2. private or protected non-static fields * 3. virtual functions * 4. base classes */ #include #include template class Array { public: typedef T* iterator; typedef const T* const_iterator; size_t size() { return N; } iterator begin() { return &arr[0]; } const_iterator begin() const { return &arr[0]; } iterator end() { return &arr[N]; } const_iterator end() const { return &arr[N]; } T& operator[] (size_t i) { return arr[i]; } T operator[] (size_t i) const { return arr[i]; } std::string ToString(); // arr must be public so that Array will be an Aggregate type. T arr[N]; }; template std::string Array::ToString() { std::ostringstream out; for(size_t i=0; i