DimovIvan

C++Advanced-Map and Set - PhoneNumbers

Mar 18th, 2022
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include<iostream>
  2. #include<map>
  3. #include<string>
  4. #include<cctype>
  5.  
  6. void printResult(const std::map<std::string, std::string>& contacts, char searchedLetter) {
  7.     int contactsCount = 0;
  8.     for (auto it = contacts.begin(); it != contacts.end(); it++)
  9.     {
  10.         if (it->first[0] == searchedLetter)
  11.         {
  12.             std::cout << it->first << " " << it->second << std::endl;
  13.             contactsCount++;
  14.         }
  15.     }
  16.     if (contactsCount == 0 )
  17.     {
  18.         std::cout << "There are no contacts with such first letter" << std::endl;
  19.     }
  20. }
  21.  
  22. int main() {
  23.     std::map<std::string, std::string>contacts = {
  24.         {"Georgi", "0886123456"},
  25.         {"Pesho", "0898435689"},
  26.         {"Petar", "0898255689"},
  27.         {"Ivan", "0876235416"},
  28.         {"Tania", "052236589"},
  29.         {"Silvia", "0982323456"},
  30.         {"Gergana", "058236448"},
  31.         {"Dimitar", "0884895456"},
  32.         {"Pavlina", "0888456789"},
  33.         {"Doncho", "0982456789"},
  34.         {"Ivanina", "0882456789"},
  35.     };
  36.     contacts.insert(std::make_pair("Iliana", "0888935416"));
  37.     contacts.erase("Petar");
  38.  
  39.     std::cout << "Enter a letter" << std::endl;
  40.     char searchedLetter;
  41.     std::cin >> searchedLetter;
  42.     if (islower(searchedLetter))
  43.     {
  44.         searchedLetter = toupper(searchedLetter);
  45.     }
  46.  
  47.     printResult(contacts, searchedLetter);
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment