/* Based on work from array list type (arrlist.h) Only holds C++ string type elements Allows assignment of string array directly to strlist type Accessible by array notation ([]) Maintains own size Warren Myers Sep 2004 WMM Jun 05 - updated handling of bogus indices into the array.. see the operator [] code WMM Oct 05 - added destructor This source is in the public domain. The original author does not make any claims as to usefulness, correctness, or suitability of this code for any purpose. Changes may be made at will and released, just keep this header in any derivative work. Interface definition: strlist strlist<> sl; strlist<210> sl; // 210 size strlist<> sl(100, array); // copy the first 100 elements of array into sl sl = array; // copy all of array into sl, if there's room; array must end with empty string ("") void empty() - reset size to empty list void clear() - reset size and reset every element to blank void clear(int pos) - like clear(), but only after pos int max() - most items you can hold in list int size() - current last location of stored item (0 based like normal C arrays) bool add(string next) - if there's room, insert next at end of list bool remove(int pos, string &res) - if exists, copy item at pos into res, then slide entire list afterward up one spot, update size bool remove(int pos) - if exists, move end item into position pos, update size void =(string []) - so long as the last element is blank (""), copies contents of 'normal' array into strlist */ #ifndef _STRLIST_H_ #define _STRLIST_H_ using namespace std; #include template class strlist{ string blank, dummy; string items[(SIZE+1)]; int len; bool ok; bool valid(int p){ if(p<0 || p>=SIZE) return false; return true; } public: strlist(){ empty(); } strlist(int s, string l[]){ if(s<1) return; if(s>SIZE) s=SIZE; len = 0; while(len0){ pos %= SIZE; // adjust for values too big } else{ pos = -pos; // this indexes from end instead of from start, is a negative index pos %= SIZE; pos = SIZE-pos; } if(pos>len){ len = pos+1; } return items[pos]; } bool add(string next){ if(SIZE == len) return false; len++; items[len] = next; return true; } bool remove(int pos, string &res){ // expensive call due to sliding contents up the list if((ok = valid(pos))){ res = items[pos]; int k = pos; while(k