// level_order.cpp, a small test of complete traversal generic algorithms #include #include #include #include #include "complete_traversal.h" using namespace std; // Here is a (contrived) example function object type // that meets the requirements of the complete traversal // generic algorithms: template struct fun { queue > Q; void operator()(int x, Container& c) { cout << x << " "; if (2*x < 100) Q.push(2*x); if (2*x+1 < 100) Q.push(2*x+1); } }; int main() { // Construct a small example set of integers and // use complete_unique_traversal on it and a specialization of fun: cout << "Example of a complete traversal of a set container:" << endl; set s1; int j; for (j = 1; j < 7; ++j) s1.insert(10*j); cout << "Original container: "; set::iterator i1; for (i1 = s1.begin(); i1 != s1.end(); ++i1) cout << " " << *i1; cout << endl; cout << "Output during traversal: "; fun > f1; complete_unique_traversal(s1, f1); cout << endl; cout << "Final container: "; for (i1 = s1.begin(); i1 != s1.end(); ++i1) cout << " " << *i1; cout << endl << endl; // Construct a small example multiset of integers and // use complete_multiple_traversal on it and a specialization of fun: cout << "Example of a complete traversal of a multiset container:" << endl; multiset s2; for (j = 1; j < 7; ++j) s2.insert(10*j); cout << "Original container: "; multiset::iterator i2; for (i2 = s2.begin(); i2 != s2.end(); ++i2) cout << " " << *i2; cout << endl; cout << "Output during traversal: "; fun > f2; complete_multiple_traversal(s2, f2); cout << endl; cout << "Final container: "; for (i2 = s2.begin(); i2 != s2.end(); ++i2) cout << " " << *i2; cout << endl << endl; return 0; }