#include #include #include #include using std::queue; using std::stack; using std::list; using std::reverse_bidirectional_iterator; void QueueExample(); void StackExample(); void ListExample(); void main() { // QueueExample(); // StackExample(); ListExample(); } void QueueExample() { queue MyInts; MyInts.push(8); MyInts.push(5); MyInts.push(5); MyInts.push(2); MyInts.push(9); while (!MyInts.empty()) { cout << MyInts.front(); MyInts.pop(); } } void StackExample() { stack MyInts; MyInts.push(8); MyInts.push(5); MyInts.push(5); MyInts.push(2); MyInts.push(9); while (!MyInts.empty()) { cout << MyInts.top(); MyInts.pop(); } } bool IsBig(int x) { return ( x >= 100 ); } void ListExample() { typedef list LISTINT; // For some reasion you have to typedef the list // to get this to work. Just using list::iterator // does not work. LISTINT::iterator IntegerIterator; LISTINT MyInts; //replaces the sequence controlled by *this with a repetition //of n elements of value x MyInts.assign(5, 100); //erase removes the element of the controlled sequence pointed to by it MyInts.erase(MyInts.begin()); //removes the elements of the controlled sequence in the range [first, last). MyInts.erase(MyInts.begin(), MyInts.end()); //clear function calls erase( begin(), end()). MyInts.clear(); // pushes on the front and back MyInts.push_back(8); MyInts.push_front(5); MyInts.push_front(5); //pushes after the first item in the list //first sets the iterator to the begining then moves //it to the next positoin, then inserts after befor the iterator postion IntegerIterator = MyInts.begin(); IntegerIterator ++; MyInts.insert(IntegerIterator,9); //use the iterator to move completly through the list //from begining to end for(IntegerIterator = MyInts.begin(); IntegerIterator != MyInts.end(); IntegerIterator++) { cout << *IntegerIterator << endl; } cout << endl; MyInts.push_front(100); MyInts.push_front(100); //use the iterator to move completly through the list //from end to begining for(IntegerIterator = MyInts.end(); IntegerIterator != MyInts.begin();) { IntegerIterator--; cout << *IntegerIterator << endl; } //Removes all items in the collection that == the argument MyInts.remove(100); //reverses the order in which elements appear in the controlled sequence MyInts.reverse(); //sorts them MyInts.sort(); MyInts.unique(); //remove and show all item from the begining to end while (!MyInts.empty()) { cout << MyInts.front() << endl; MyInts.pop_front(); } // or //remove and show all item from the end to begining while (!MyInts.empty()) { cout << MyInts.back() << endl; MyInts.pop_back(); } }