fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6.  
  7. struct ContactItem
  8. {
  9. string strContactsName;
  10. string strPhoneNumber;
  11.  
  12. ContactItem(const string& strName, const string & strNumber)
  13. {
  14. strContactsName = strName;
  15. strPhoneNumber = strNumber;
  16. }
  17.  
  18. bool operator == (const ContactItem& itemToCompare) const
  19. {
  20. return (itemToCompare.strContactsName == this->strContactsName);
  21. }
  22.  
  23. bool operator < (const ContactItem& itemToCompare) const
  24. {
  25. return (this->strContactsName < itemToCompare.strContactsName);
  26. }
  27.  
  28. };
  29.  
  30.  
  31.  
  32. void FindContact(const set <ContactItem>& setContacts)
  33. {
  34. cout << "*** Wyszukanie danych kontaktowych ***" << endl;
  35. cout << "Czyj numer telefonu chcialbys znalezc?" << endl;
  36. cout << "> ";
  37. string strName;
  38. cin >> strName;
  39.  
  40. set <ContactItem>::const_iterator iContactFound
  41. = setContacts.find(ContactItem(strName, ""));
  42.  
  43. if (iContactFound != setContacts.end())
  44. {
  45. cout << strName << " jest dostepny pod numerem telefonu: ";
  46. cout << iContactFound->strPhoneNumber << endl;
  47. }
  48. else
  49. cout << strName << " nie zostal znaleziony na liscie kontaktow" << endl;
  50.  
  51. cout << endl;
  52.  
  53. return;
  54. }
  55.  
  56. class FindByNumber
  57. {
  58. string number;
  59. public:
  60. FindByNumber(string _number):number(_number) { }
  61. bool operator() (const ContactItem& it) { return it.strPhoneNumber==number; }
  62.  
  63. };
  64.  
  65. void FindContactByNumber(const set <ContactItem>& setContacts)
  66. {
  67.  
  68. cout << "Znajdz imie kontaktu po numerze telefonu!" << endl;
  69. cout << "Wpisz numer telefonu" << endl;
  70. cout << ">";
  71. string strPhoneNumber;
  72. cin >> strPhoneNumber;
  73.  
  74. auto iContactFound = find_if(setContacts.begin(), setContacts.end(), FindByNumber(strPhoneNumber));
  75.  
  76. if (iContactFound != setContacts.end())
  77. {
  78. cout << strPhoneNumber << " jest numerem telefonu do: ";
  79. cout << iContactFound->strContactsName << endl;
  80. }
  81. else
  82. {
  83. cout << "Kontakt o podanym numerze telefonu nie zostal znaleziony!" << endl;
  84. }
  85.  
  86. }
  87.  
  88. int main() {
  89. set<ContactItem> ksiazka;
  90. ksiazka.insert(ContactItem("Stasiu", "1234"));
  91. FindContactByNumber(ksiazka);
  92.  
  93. return 0;
  94. }
Success #stdin #stdout 0s 3440KB
stdin
1234
stdout
Znajdz imie kontaktu po numerze telefonu!
Wpisz numer telefonu
>1234 jest numerem telefonu do: Stasiu