Advertisement
Guest User

Untitled

a guest
May 6th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <functional>
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. class AddressBook
  10. {
  11. public:
  12. // using a template allows us to ignore the differences between functors, function pointers
  13. // and lambda
  14. template<typename Func>
  15. std::vector<std::string> findMatchingAddresses (Func func)
  16. {
  17. std::vector<std::string> results;
  18. for ( auto itr = _addresses.begin(), end = _addresses.end(); itr != end; ++itr )
  19. {
  20. // call the function passed into findMatchingAddresses and see if it matches
  21. if ( func( *itr ) )
  22. {
  23. results.push_back( *itr );
  24. }
  25. }
  26. return results;
  27. }
  28.  
  29. std::vector<std::string> _addresses = {
  30. "Alpha@networkone.com",
  31. "Bravo@networkone.com",
  32. "Charlie@networkone.com",
  33. "Delta@networkone.com",
  34. "Echo@networkone.com",
  35. "Foxtrot@networkone.com",
  36. "Golf@networkone.com",
  37. "Hotel@networktwo.com",
  38. "India@networktwo.com",
  39. "Juliet@networktwo.org",
  40. "Kilo@networktwo.com",
  41. "Lima@networktwo.com",
  42. "Mike@networktwo.com",
  43. "November@networktwo.com",
  44. "Oscar@networktwo.com",
  45. "Papa@networkthree.com",
  46. "Quebec@networkthree.com",
  47. "Romeo@networkthree.org",
  48. "Sierra@networkthree.com",
  49. "Tango@networkthree.com",
  50. "Uniform@networkthree.com",
  51. "Victor@networkthree.com",
  52. "Whiskey@networkthree.com",
  53. "X-ray@networkthree.com",
  54. "Yankee@networkthree.com",
  55. "Zulu@networkthree.com"};
  56. };
  57.  
  58. AddressBook global_address_book;
  59.  
  60. vector<string> findAddressesFromOrgs ()
  61. {
  62.  
  63. auto lambda = [](std::string address) {
  64. std::size_t found = address.find(".org");
  65. if (found!=std::string::npos)
  66. return true;
  67. return false;
  68.  
  69. /*
  70. krócej:
  71. return address.find(".org" != std::string::npos)*/
  72. };
  73.  
  74. return global_address_book.findMatchingAddresses(lambda);
  75. /*
  76. 1. @todo
  77. Write a body of this function.
  78. Make it return a vector of strings which are all addresses
  79. from global_address_book that contain '.org',
  80. using findMatchingAddresses method
  81. */
  82. }
  83.  
  84. vector<string> findAddressesFromName ()
  85. {
  86. std::string name;
  87. std::cin >> name;
  88.  
  89. return global_address_book.findMatchingAddresses([&]
  90. (std::string address) {return address.find(name) != std::string::npos;});
  91. /*
  92. 2. @todo
  93. Write a body of this function.
  94. Read in name of a user from standard input.
  95. Make it return a vector of strings which are all addresses
  96. from global_address_book that contain the specified name,
  97. using findMatchingAddresses method.
  98. */
  99. }
  100.  
  101. vector<string> findAddressesMinLen (int min_len)
  102. {
  103. return global_address_book.findMatchingAddresses([]
  104. (std::string address) {return address.length() >= min_len; });
  105.  
  106. /*
  107. 3. @todo
  108. Write a body of this function.
  109. Make it return a vector of strings which are all addresses
  110. from global_address_book that are longer or equal to min_len,
  111. using findMatchingAddresses method.
  112. */
  113. }
  114.  
  115. int main()
  116. {
  117. vector<string> v1 = findAddressesFromOrgs();
  118. vector<string> v2 = findAddressesFromName();
  119. vector<string> v3 = findAddressesMinLen(21);
  120.  
  121. cout << "---v1---" << endl;
  122. for ( auto itr = v1.begin(), end =v1.end(); itr != end; ++itr )
  123. cout << *itr << endl;
  124.  
  125. cout << "--v2---" << endl;
  126. for ( auto itr = v2.begin(), end =v2.end(); itr != end; ++itr )
  127. cout << *itr << endl;
  128.  
  129. cout << "--v3---" << endl;
  130. for ( auto itr = v3.begin(), end =v3.end(); itr != end; ++itr )
  131. cout << *itr << endl;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement