#include #include using namespace std; class myC { public: myC() : key(0), s("") {} myC(int ky, string str) : key(ky), s(str) {} //myC(const myC& c); // Copy constructor not req //void operator=(const myC& r); // op= not required int getKey() {return key;} void setKey(int k) {key = k;} string getString() { return s;} void setString(string str) {s = str;} private: int key; string s; }; typedef list myList; typedef list::iterator listIterator; int main () { string phrase[]={"We're #1", "We try harder", "We placed", "We Lost"}; myList l; myC c, cfirst, clast; int i; listIterator li; for (i = 1; i <= 4; i++) { c = myC(i, phrase[i-1]); if (i % 2 == 1) { l.push_back(c); } else { l.push_front(c); } } for (li = l.begin(), i = 1; li != l.end(); li++, i++) { cout << "Element " << i << ": key = " << li->getKey() << ", s = " << li->getString() << "." << endl; } // back up to the element that should be first, and get a copy, then erase it li--; li--; cfirst = *li; l.erase(li); // get a copy of the front element, erase it, and put it at the end where it // belongs. clast = l.front(); l.erase(l.begin()); l.push_back(clast); // now put the element the should be first on the list l.push_front(cfirst); // Take note that what goes into the list is a copy of the original cfirst.setString("We're number one"); for (li = l.begin(), i = 1; li != l.end(); li++, i++) { cout << "Element " << i << ": key = " << li->getKey() << ", s = " << li->getString() << "." << endl; } return 0; }