文件一:Person.h
//Person.h
#pragma once
#include <string>
using std::string;
class Person{
public:
Person(const string& first="",const string& second="")
:firstname(first),secondname(second){
}
Person(string&& first, string&& second):
firstname(std::move(first)),secondname(std::move(second)){
}
//less-than operator
bool operator<(const Person& p)const{
return (secondname<p.secondname ||
((secondname == p.secondname) && (firstname <p.firstname)));
}
string getName() const{
return firstname + "" + secondname;
}
private :
string firstname;
string secondname;
};
主程序文件.cpp
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <tuple>
#include <iomanip>
#include <array>
#include <map>
#include "Person.h"
using std::cin;
using std::cout;
using std::endl;
using std::tuple;
using std::string;
using std::setw;
using std::get;
using std::pair;
using std::map;
typedef std::map<Person,string> PhoneBook;
typedef std::pair<Person,string> Entry;
Person getPerson(){
string first,second;
cout<<"Enter a first name:";
getline(cin,first);
cout<<"Enter a second name:";
getline(cin,second);
return Person(first,second);
}
Entry inputEntry(){
Person person = getPerson();
string number;
cout<<"输入"<<person.getName()<<"的电话号码:";
getline(cin,number);
return std::make_pair(std::move(person),std::move(number));
}
void addEntry(PhoneBook& book){
Entry entry = inputEntry();
auto pr = book.insert(std::move(entry));
if(pr.second)
{
cout<<"Entry succesful." << endl;
}
else
{
cout<<"Entry exists for" << pr.first->first.getName()
<<". The number is "<< pr.first->second <<endl;
}
}
void listEntries(const PhoneBook& book){
if(book.empty()){
cout<<"The phone book is empty."<<endl;
return;
}
cout<< std::setiosflags(std::ios::left);
for(auto& entry:book)
{
cout<< setw(30) <<entry.first.getName()
<<setw(12) <<entry.second<<endl;
}
cout<<std::resetiosflags(std::ios::right);
}
void getEntry(const PhoneBook& book){
Person person = getPerson();
auto iter = book.find(person);
if(iter == book.end())
cout<<"No entry found for " <<person.getName()<<endl;
else
cout<<"The number for " <<person.getName() << "is " <<iter->second<<endl;
}
void deleteEntry(PhoneBook& book){
Person person = getPerson();
auto iter = book.find(person);
if(iter == book.end())
{
cout<< " no entry found for " <<person.getName()<<endl;
}
else
{
book.erase(iter);
cout<<person.getName() << " erased." <<endl;
}
}
int main(int argc,_TCHAR* argv[])
{
PhoneBook phoneBook;
char answer=0;
while (true)
{
cout << "Do youwant to enter a phone book entry(Y or N):";
cin>>answer;
cin.ignore(); //ignore new line in buffer;
if(toupper(answer) == 'N')
break;
if(toupper(answer) != 'Y')
{
cout<< "Invalid response ,Try again." <<endl;
continue;
}
addEntry(phoneBook);
}
//Query the phonebook
while (true)
{
cout<<endl<<"make a choice:"<<endl
<<"A Add an entry "<<endl
<<"D Delete an entry"<<endl
<<"G Get an entry"<<endl
<<"L List entries"<<endl
<<"Q Quit"<<endl;
cin >>answer;
cin.ignore();
switch (toupper(answer))
{
case 'A':
addEntry(phoneBook);
break;
case 'G':
getEntry(phoneBook);
break;
case 'D':
deleteEntry(phoneBook);
break;
case 'L':
listEntries(phoneBook);
break;
case 'Q':
return 0;
default:
cout<<"别乱选,重来"<<endl;
break;
}
}
return 0;
}