Created
June 25, 2021 09:52
-
-
Save DreamVB/0688dd7561c6097792e9789622d3eaf0 to your computer and use it in GitHub Desktop.
Phone Book Map Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Phone book example using std map | |
#include <iostream> | |
#include <string> | |
#include <map> | |
using namespace std; | |
using std::string; | |
class TPhoneBook{ | |
private: | |
//Hold the phone data | |
std::map<string, string>Data; | |
public: | |
//Used to serach the map | |
std::map<string, string>::iterator it; | |
//Add new contact to the phone book | |
void add_contact(std::string name, std::string phone){ | |
//Set map data | |
Data[name] = phone; | |
} | |
void edit_contact(std::string name, std::string phone){ | |
//Find the name in the data map | |
it = Data.find(name); | |
//Not there so display errror | |
if (it == Data.end()){ | |
std::cout << name << " was not found in the phone book." << std::endl; | |
} | |
else{ | |
//Update the phone number | |
Data[name] = phone; | |
} | |
} | |
void delete_contact(std::string name){ | |
//Locate name in the Data map | |
it = Data.find(name); | |
//Not there so display error | |
if (it == Data.end()){ | |
std::cout << "[Delete Contact]" << std::endl; | |
std::cout << name << " was not found in the phone book." << std::endl; | |
} | |
else{ | |
//Remove the item from the Data map | |
Data.erase(it); | |
} | |
} | |
void find_phone_byName(std::string name){ | |
//Find name in the Data map | |
it = Data.find(name); | |
//Not found so display error | |
if (it == Data.end()){ | |
std::cout << name << " was not found in the phone book." << std::endl; | |
} | |
else{ | |
//Display phone contact found data | |
std::cout << std::endl << "[Found Record]" << std::endl; | |
std::cout << "Name : " << it->first << std::endl; | |
std::cout << "Phone : " << it->second << std::endl; | |
std::cout << "--------------------------" << std::endl; | |
} | |
} | |
//Show all records in the phone book. | |
void display_all(void){ | |
//Display all the pnone contacts in the Data map | |
std::cout << "[Records]" << std::endl; | |
for (it = Data.begin(); it != Data.end(); it++){ | |
std::cout << "Name : " << it->first << std::endl << "Phone : " << | |
it->second << std::endl; | |
std::cout << "--------------------------" << std::endl; | |
} | |
} | |
}; | |
int main(){ | |
//Create the phone book class | |
TPhoneBook book; | |
//Add some contacts | |
book.add_contact("Paul", "0800-123456"); | |
book.add_contact("Zoe", "7895-4578961"); | |
book.add_contact("Ben", "1234-456789"); | |
//Show all contacts | |
book.display_all(); | |
//Edit contact Paul phone number | |
book.edit_contact("Paul", "01252 710992"); | |
//Locate Paul in the phone book and show his record | |
book.find_phone_byName("Paul"); | |
//Remove the contact Zoe | |
book.delete_contact("Zoe"); | |
//Show all phone contacts agian | |
book.display_all(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment