#include #include #include #include using namespace std; class ma_iterator { int *spot; int remaining; public: ma_iterator(int *s, int r) : spot(s), remaining(r) {} int access(){ return *spot; } int change(int newval){ return *spot = newval; } bool iterate(){ if(remaining){ remaining--; spot++; return true; } return false; } bool done(){ return remaining; } }; class managed_array { int* numbers; public: int numbers_held = 0; int capacity = 20; managed_array() { numbers = new int[20]; } ma_iterator get_iterator(){ return ma_iterator(numbers, numbers_held); } int access(int index){ return numbers[index]; } int modify(int index, int newvalue){ numbers[index] = newvalue; return newvalue; } int search(int value){ for(int i = 0; i < numbers_held; i++) if(numbers[i] == value) return i; return -1; } void remove(int index){ numbers_held--; for(int i = index; i < numbers_held - 1; i++) numbers[i] = numbers[i+1]; } void map(void (*f)(int)){ for(int i = 0; i < numbers_held; i++) f(numbers[i]); } string to_string_original(){ char result[1024]; int howfar = 0; howfar += sprintf(result, "["); for(int i = 0; i < numbers_held-1; i++){ howfar += sprintf(result + howfar, "%d, ", numbers[i]); } howfar += sprintf(result+howfar, "%d]", numbers[numbers_held-1]); return string(result); } /* "c++ style" implementation using string streams * I'd recommend this one for c++ programs in preference to the above * Note that the above C-style will probably run faster, but will fail if * the array doesn't fit in 1024 characters. It'd be easy enough to allocate * enough space above, something like numbers_held * 16. */ string to_string(){ stringstream stream; stream << "["; for(int i = 0; i < numbers_held-1; i++) stream << numbers[i] << ", "; stream << numbers[numbers_held-1] << "]"; return stream.str(); } void add(int new_number) { if(numbers_held >= capacity){ cout << "20 numbers reached, resizing to 40\n"; int *old_numbers = numbers; numbers = new int[40]; for(int i = 0; i < 20; i++) numbers[i] = old_numbers[i]; delete [] old_numbers; capacity = 40; } numbers[numbers_held++] = new_number; } /* We'll add this next class */ ~managed_array() { delete [] numbers; } }; void print_number(int p){ cout << p << endl; } int main(){ managed_array am1; managed_array am2; am1.add(3); am1.add(5); am1.add(77); am1.add(9); am1.modify(0, 951); for(int i = 0; i < 20; i++) am1.add(i); cout << "am1 is holding : " << am1.numbers_held << " numbers\n"; am1.map(print_number); if(am1.search(77) != -1) cout << "77 is in the array\n"; else cout << "77 is not in the array\n"; am1.remove(2); cout << "After removing item 2, am1 is holding : " << am1.numbers_held << " numbers\n"; am1.map(print_number); if(am1.search(77) != -1) cout << "77 is in the array\n"; else cout << "77 is not in the array\n"; cout << "Our array is: " << am1.to_string() << endl; for(ma_iterator it = am1.get_iterator(); it.done(); it.iterate()){ it.change(it.access() * 2); } cout << "Our array is: " << am1.to_string() << endl; return 0; }