Code:
#include <map>
#include <list>
#include <string>
#include <iostream>
//#include <algorithm>
using namespace std;
// for case insensitive ordering of the multimap
struct lessStrNoCase{
bool operator() (const string s1, const string s2) const {
return stricmp(s1.c_str(), s2.c_str()) < 0;
}
};
typedef multimap <unsigned long, string> NumberNameMap;
typedef multimap <string, unsigned long, lessStrNoCase> NameNumberMap;
typedef pair <unsigned long, string> NumberNamePair;
typedef pair <string, unsigned long> NameNumberPair;
typedef NumberNameMap::iterator NumberNameMapIt;
typedef NameNumberMap::iterator NameNumberMapIt;
typedef list<NumberNameMapIt> NumberNameMapItList;
typedef list<NameNumberMapIt> NameNumberMapItList;
// functions to search in the multimaps Return lists of iterators to the multimaps
NumberNameMapItList FindByNumber(NumberNameMap* nmap, unsigned long num);
NameNumberMapItList FindByName(NameNumberMap* nmap, string name);
int main()
{
// set up the mock telephone directory
NumberNameMap numbers_map; // for searching by number
NameNumberMap names_map; // for searching by name
// people and their phone numbers to add to the directory
string names[] = {"John", "angela", "Juanita", "Alex", "Alexander",
"Juan", "Julie", "Julie", "Juanito", "Daniel",
"Mary", "Mary", "Mary", "Brian", "Earl",
"Nate", "Nathan", "James", "Bryan", "Bing",
"Hiram", "Lizbeth", "Dietrich", "Anna", "Scott"};
unsigned long nums[] = {7712323, 7715874, 7712323, 7714730, 7710386,
7751654, 7852145, 7854125, 7759987, 7714804,
2782326, 2789854, 2785076, 2787451, 7714007,
7719874, 7751696, 2785176, 2785176, 2785176,
7714444, 7758888, 7718051, 7714895, 7712323};
const int NUMENTRIES = 25;
// add names and numbers to the two directories.
// (one indexed by the name, and the other by the phone number)
for(int i=0; i<NUMENTRIES; ++i)
numbers_map.insert(NumberNamePair(nums[i], names[i]));
for(i=0; i<NUMENTRIES; ++i)
names_map.insert(NameNumberPair(names[i], nums[i]));
// output all ordered by Phone number
cout << "==================================\n";
for (NumberNameMapIt num_it = numbers_map.begin(); num_it != numbers_map.end(); ++num_it)
cout << "Phone number: " << (*num_it).first << " Name: " << (*num_it).second << "\n";
cout << "==================================\n";
// output all ordered by Name
for (NameNumberMapIt name_it = names_map.begin(); name_it != names_map.end(); ++name_it)
cout << "Name: " << (*name_it).first << "\t\tPhone number: " << (*name_it).second << "\n";
cout << "==================================\n";
// output a search by number
unsigned long some_number = 2785176;
NumberNameMapItList ll = FindByNumber(&numbers_map, some_number);
if( ll.empty() ){
cout << "No matches for " << some_number << "\n";
}else{
cout << "There were " << ll.size() << " matches for " << some_number << ", here they are:\n";
for(; !ll.empty(); ll.pop_front() )
cout << ll.front()->first << " " << ll.front()->second << endl;
}
cout << "==================================\n";
// output a search by name
string some_name = "Julie";
NameNumberMapItList thelist = FindByName(&names_map, some_name);
if( thelist.empty() ){
cout << "No matches for " << some_name << "\n";
}else{
cout << "There were " << thelist.size() << " matches for " << some_name << ", here they are:\n";
for(; !thelist.empty(); thelist.pop_back() )
cout << thelist.back()->first << " " << thelist.back()->second << endl;
}
cout << "==================================\n";
return 0;
}
NumberNameMapItList FindByNumber(NumberNameMap* nmap, unsigned long num)
{
NumberNameMapItList nnmil;
pair<NumberNameMapIt, NumberNameMapIt> range;
for( range = nmap->equal_range( num );
range.first != range.second && range.first != nmap->end();
++range.first ){
nnmil.push_back( range.first );
//cerr << "range.first " << range.first->first << " " << range.first->second << endl;
}
return nnmil;
}
NameNumberMapItList FindByName(NameNumberMap* nmap, string name)
{
NameNumberMapItList nnmil;
pair<NameNumberMapIt, NameNumberMapIt> range;
for( range = nmap->equal_range( name );
range.first != range.second && range.first != nmap->end();
++range.first ){
nnmil.push_back( range.first );
//cerr << "range.first " << range.first->first << " " << range.first->second << endl;
}
return nnmil;
}
output: