00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "load_binary_tree_from_random_access_iterator.h"
00026
00027 #include <iostream>
00028 #include <string>
00029 #include <map>
00030 #include <iterator>
00031 #include <vector>
00032
00033 using namespace std;
00034
00037 struct employee {
00038 int ssn;
00039 string name;
00040
00041 bool operator< ( const employee& rhs) const
00042 {
00043 return ssn < rhs.ssn;
00044 }
00045
00046 friend ostream& operator << ( ostream& os, const employee& e);
00047 };
00048
00049 ostream& operator << ( ostream& os, const employee& e) {
00050 os << "{ " << e.ssn << ", " << e.name << "}";
00051 return os;
00052 }
00053
00054 std::pair<int,employee> create_map_pair( const employee& e)
00055 {
00056 return std::make_pair( e.ssn, e);
00057 }
00058
00059 ostream& operator << ( ostream& os, const std::pair<int,employee>& e) {
00060 os << e.second;
00061 return os;
00062 }
00063
00066 struct Create_Map_Pair {
00067 int id_offset;
00068
00069 Create_Map_Pair( int id_offset) : id_offset( id_offset)
00070 {
00071 }
00072
00073 Create_Map_Pair( const Create_Map_Pair& rhs) : id_offset( rhs.id_offset) {
00074 }
00075
00076 inline std::pair<int,employee> operator()( const employee& e)
00077 {
00078 return std::make_pair( e.ssn + id_offset, e);
00079 }
00080 };
00081
00082 employee workers[] = {
00083 { 1, "Jack"},
00084 { 2, "Harry"},
00085 { 3, "Joe"},
00086 { 4, "Jack"},
00087 { 5, "Harry"},
00088 { 6, "Joe"},
00089 { 7, "Jack"},
00090 { 8, "Harry"},
00091 { 9, "Joe"},
00092 };
00093
00094 int main() {
00095 map<int,employee> a_map;
00096
00097 size_t number_workers = sizeof(workers) / sizeof(employee);
00098 const vector<employee> Workers( &workers[0], &workers[number_workers]);
00099
00100 Create_Map_Pair cmp( 1000);
00101
00102 breadth_first_load_binary_tree_from_random_access_iterator( Workers.begin(), Workers.end(), a_map, cmp);
00103
00104 cout << "results from calling functor cmp for a map" << endl;
00105 std::copy(a_map.begin(), a_map.end(), std::ostream_iterator<std::pair<int,employee> >(std::cout, "\n"));
00106
00107 breadth_first_load_binary_tree_from_random_access_iterator( Workers.begin(), Workers.end(), a_map, create_map_pair);
00108
00109 cout << "results from calling function create_map_pair a map" << endl;
00110 std::copy(a_map.begin(), a_map.end(), std::ostream_iterator<std::pair<int,employee> >(std::cout, "\n"));
00111
00112 multimap<int,employee> a_multimap;
00113
00114 breadth_first_load_binary_tree_from_random_access_iterator( Workers.begin(), Workers.end(), a_multimap, cmp);
00115
00116 cout << "results from calling functor cmp for a multimap" << endl;
00117 std::copy(a_multimap.begin(), a_multimap.end(), std::ostream_iterator<std::pair<int,employee> >(std::cout, "\n"));
00118
00119 cout << "results from calling function create_map_pair a multimap" << endl;
00120 std::copy(a_multimap.begin(), a_multimap.end(), std::ostream_iterator<std::pair<int,employee> >(std::cout, "\n"));
00121
00122
00123 return 0;
00124 }