view algs4-c++/src/Queue.cpp @ 27:80ca1973e3bd

Fleshed out Queue::generic_iterator a bit more to make it a more or less complete example of implmenting an iterator.
author Eris Caffee <discordia@eldalin.com>
date Tue, 23 Jun 2015 17:14:09 -0500
parents 028689700a47
children
line source
1 // g++ -std=c++11 Queue.cpp
4 #include <iostream>
5 #include <typeinfo>
7 #include "Queue.hpp"
9 int main ( int argc, char **argv ) {
11 Queue<long> queue;
13 long i;
14 while ( ! std::cin.eof() ) {
15 std::cin >> i;
16 if ( std::cin.good() )
17 if ( i >= 0 )
18 queue.enqueue(i);
19 else
20 queue.dequeue();
21 }
23 std::cout << "Queue has " << queue.size() << " entries." << std::endl;
25 for ( auto iter = queue.begin(); iter != queue.end(); ++iter ) {
26 std::cout << *iter << std::endl;
27 }
29 Queue<long> q2( queue );
30 std::cout << "Copied queue to q2. q2 has " << q2.size() << " entries." << std::endl;
31 for ( auto iter = q2.begin(); iter != q2.end(); ++iter ) {
32 std::cout << *iter << std::endl;
33 }
35 // Just make sure that we can build an iterator with the copy constructor.
36 auto iter = q2.begin();
37 auto iter2( iter );
39 std::cout << "Dequeuing entries..." << std::endl;
41 while ( ! queue.is_empty() ) {
42 i = queue.dequeue();
43 std::cout << i << std::endl;
44 }
46 std::cout << "Queue has " << queue.size() << " entries." << std::endl;
49 // Silently empty q2 to avoid the assertion in the destructor.
50 while ( ! q2.is_empty() )
51 q2.dequeue();
53 // Show the iterator traits;
54 std::cout << std::endl;
55 if ( typeid( Queue<long>::iterator_traits::iterator_category) == typeid( std::forward_iterator_tag ) )
56 std::cout << "category: forward iterator" << std::endl;;
57 if ( typeid( Queue<long>::iterator_traits::value_type) == typeid( long ) )
58 std::cout << "value_type: long" << std::endl;;
59 if ( typeid( Queue<long>::iterator_traits::pointer) == typeid( long * ) )
60 std::cout << "pointer: long *" << std::endl;;
61 if ( typeid( Queue<long>::iterator_traits::reference) == typeid( long & ) )
62 std::cout << "reference: long &" << std::endl;;
63 if ( typeid( Queue<long>::iterator_traits::difference_type ) == typeid( ptrdiff_t ) )
64 std::cout << "difference_type: ptrdiff_t" << std::endl;;
65 }