// test_list.cpp just demo some features, not a full test #include #include using namespace std; typedef list my_list; // give my typename to a list of integers // this simplifies code for function prototypes, etc. void put(const my_list L) // function to output a list // of type my_list { cout << "L(" << L.size() << ")= "; // use begin() end() to be safe for(my_list::const_iterator p=L.begin(); p!=L.end(); p++) cout << *p << " "; cout << endl; } int main() { my_list L1; // construct an empty list my_list L2; my_list L3; cout << "test_list running, L is " << (L1.empty() ? "empty" : "has junk") << " of max size= " << L1.max_size() << endl; L1.push_front(3); L1.push_front(5); L1.push_back(1); L1.push_back(2); L2.push_front(4); L2.push_front(2); L2.push_front(6); cout << "initial L1 "; put(L1); // size() begin() ens() demonstrated in 'put()' L1.sort(); // sort the list cout << "sorted L1 "; put(L1); L1.reverse(); // reverse order of list cout << "reversed L1 "; put(L1); L1.pop_front(); // removes first item on list cout << "L1.pop_front() "; put(L1); cout << "initial L2 "; put(L2); L2.pop_back(); // removes last item on list cout << "L2.pop_back() "; put(L2); L3.insert(L3.end(), L1.begin(), L1.end()); L3.insert(L3.end(), L2.begin(), L2.end()); cout << "L3.insert L1 and L2 "; put(L3); L1.merge(L2); cout << "L1.merge(L2) "; put(L1); cout << "L2 is erased "; put(L2); L3.splice(++L3.begin(), L1); cout << "L3.splice ++ L1 "; put(L3); cout << "L1 is erased "; put(L1); return 0; } // Output of this program is: // test_list running, L is empty of max size= 1073741823 // initial L1 L(4)= 5 3 1 2 // sorted L1 L(4)= 1 2 3 5 // reversed L1 L(4)= 5 3 2 1 // L1.pop_front() L(3)= 3 2 1 // initial L2 L(3)= 6 2 4 // L2.pop_back() L(2)= 6 2 // L3.insert L1 and L2 L(5)= 3 2 1 6 2 // L1.merge(L2) L(5)= 3 2 1 6 2 // L2 is erased L(0)= // L3.splice ++ L1 L(10)= 3 3 2 1 6 2 2 1 6 2 // L1 is erased L(0)=