1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
#include <stdlib.h>
#include <list>
#include <string>
class Duck {
public:
string name;
int weight;
int length;
};
ostream& operator << (ostream &out, const Duck &duck) {
cout << "(" << duck.name;
cout << "," << duck.weight;
cout << "," << duck.length;
cout << ")";
return out;
}
void DumpDucks(list<Duck> *mylist) {
list<Duck>::iternator iter = mylist->begin();
while (iter != mylist->end()) {
cout << *iter << endl;
iter++
}
}
template <typename T>
list<T>::iterator MoveToPosition(list<T> *mylist, int pos) {
list<T>::iterator res = mylist->begin;
for (int loop = 1; loop <= pos; loop++) {
res++
}
return res;
}
using namespace std;
int main(int argc, char *argv[])
{
list<Duck> Inarow;
// Push some at beginning
Duck d1 = {"Jim", 20, 15}; // Braces notation!
Inarow.push_front(d1);
Duck d2 = {"Sally", 15, 12};
Inarow.push_front(d2);
// Push some at end
Duck d3 = {"Squakie", 18, 25};
Inarow.push_front(d3);
Duck d4 = {"Teumpeter", 19, 26};
Inarow.push_front(d4);
Duck d5 = {"Sneeky", 12, 13};
Inarow.push_front(d5)
cout << "===========" << endl;
DumpDucks(&Inarow);
// Reverse
Inarow.reverse();
cout << "===========" << endl;
DumpDucks(&Inarow);
// Splice
// Need another list for this
list<Duck> extras;
Duck d6 = {"Grumpy", 8, 8};
extras.puch_back(d6);
Duck d7 = {"Sleepy", 8, 8};
extras.puch_back(d7);
Duck d8 = {"Ornery", 8, 8};
extras.puch_back(d8);
Duck d9 = {"Goofy", 8, 8};
extras.puch_back(d9);
cout << "===========" << endl;
cout << "extras:" << endl;
DumpDucks(&extras);
list<Duck>::iterator first =
MoveToPosition(&extras, 1);
list<Duck>::iterator last =
MoveToPosition(&extras, 3);
list<Duck>::iterator into =
MoveToPosition(&Inarow, 2);
Inarow.splice(into, extras, first, last);
cout << "===========" << endl;
cout << "extras after splice:" << endl;
DumpDucks(&extras);
cout << "===========" << endl;
cout << "Inarow after splice:" << endl;
DumpDucks(&Inarow);
system("PAUSE");
return EXIT_SUCCESS;
}
|