Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
tele.cpp
Go to the documentation of this file.
1
29#include <map>
30#include <algorithm>
31#include <vector>
32#include <string>
33#include <iostream>
34
35#include <examples.h>
36
37typedef std::map<std::string, long, std::less<std::string>,
38 std::allocator<std::pair<const std::string, long> > > friendMap;
39typedef std::map<long, std::string, std::less<long>,
40 std::allocator<std::pair<const long, std::string> > > sortedMap;
41
42typedef friendMap::value_type entry_type;
43typedef sortedMap::value_type sorted_entry_type;
44
45
46void printEntry (const entry_type & entry) {
47
48 std::cout << entry.first << ":" << entry.second << std::endl;
49}
50
51
52void printSortedEntry (const sorted_entry_type & entry) {
53
54 std::cout << entry.first << ":" << entry.second << std::endl;
55}
56
57
58long prefix (const entry_type& entry) {
59 return entry.second / 10000;
60}
61
62
63bool prefixCompare (const entry_type & a, const entry_type & b) {
64
65 return prefix (a) < prefix (b);
66}
67
68
70public:
72 { }
74 bool operator () (const entry_type& entry) {
75 return prefix (entry)==testPrefix;
76 }
77};
78
80public:
81 void addEntry (std::string name, long number) {
82 database[name] = number;
83 }
84 void remove (std::string name) {
85 database.erase (name);
86 }
87 void update (std::string name, long number) {
88 remove (name);addEntry (name, number);
89 }
91 std::for_each (database.begin (), database.end (), printEntry);
92 }
93 void displayPrefix (int);
94 void displayByPrefix ();
95private:
96 friendMap database;
97};
98
99
101
102 std::cout << "Listing for prefix " << pfx << std::endl;
103
104 friendMap::iterator
105 where = std::find_if (database.begin (), database.end (),
106 checkPrefix (pfx));
107 while (where != database.end ()) {
108 printEntry (*where);
109 where = std::find_if (++where, database.end (), checkPrefix (pfx));
110 }
111
112 std::cout << "end of prefix listing" << std::endl;
113}
114
115
117
118 std::cout << "Display by prefix" << std::endl;
119
120 sortedMap sortedData;
121 for (friendMap::iterator i = database.begin (); i != database.end (); i++)
122 sortedData.insert (sortedMap::value_type ((*i).second, (*i).first));
123 std::for_each (sortedData.begin (), sortedData.end (), printSortedEntry);
124
125 std::cout << "end display by prefix" << std::endl;
126}
127
128
129int main () {
130
131 std::cout << "Telephone Directory sample program" << std::endl;
132
133 telephoneDirectory friends;
134 friends.addEntry ("Samantha", 6342343);
135 friends.addEntry ("Brenda", 5436546);
136 friends.addEntry ("Fred", 7435423);
137 friends.addEntry ("Allen", 6348723);
138 friends.displayDatabase ();
139 friends.displayPrefix (634);
140 friends.displayByPrefix ();
141
142 std::cout << "End of telephone directory sample program" << std::endl;
143
144 return 0;
145}
#define i
Definition 17.names.cpp:126
#define a
Definition 17.names.cpp:109
#define b
Definition 17.names.cpp:110
#define p
Definition 17.names.cpp:132
bool operator()(const entry_type &entry)
Definition tele.cpp:74
checkPrefix(long p)
Definition tele.cpp:71
long testPrefix
Definition tele.cpp:73
void update(std::string name, long number)
Definition tele.cpp:87
void remove(std::string name)
Definition tele.cpp:84
void addEntry(std::string name, long number)
Definition tele.cpp:81
void displayByPrefix()
Definition tele.cpp:116
void displayDatabase()
Definition tele.cpp:90
void displayPrefix(int)
Definition tele.cpp:100
const char * name
Definition codecvt1.cpp:72
sortedMap::value_type sorted_entry_type
Definition tele.cpp:43
std::map< long, std::string, std::less< long >, std::allocator< std::pair< const long, std::string > > > sortedMap
Definition tele.cpp:40
void printEntry(const entry_type &entry)
Definition tele.cpp:46
void printSortedEntry(const sorted_entry_type &entry)
Definition tele.cpp:52
bool prefixCompare(const entry_type &a, const entry_type &b)
Definition tele.cpp:63
long prefix(const entry_type &entry)
Definition tele.cpp:58
int main()
Definition tele.cpp:129
std::map< std::string, long, std::less< std::string >, std::allocator< std::pair< const std::string, long > > > friendMap
Definition tele.cpp:38
friendMap::value_type entry_type
Definition tele.cpp:42