Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4.  
  5.  
  6. class AddressBook {
  7. public:
  8.  
  9. AddressBook() = default;
  10.  
  11. AddressBook(std::initializer_list<std::string> list) : _addresses(list.begin(), list.end()) {}
  12.  
  13. // using a template allows us to ignore the differences between functors, function pointers and lambda
  14. template<typename Func>
  15. std::vector<std::string> findMatchingAddresses(Func func) {
  16. std::vector<std::string> results;
  17. for (auto itr = _addresses.begin(), end = _addresses.end(); itr != end; ++itr) {
  18. // call the function passed into findMatchingAddresses and see if it matches
  19. if (func(*itr)) {
  20. results.push_back(*itr);
  21. }
  22. }
  23. return results;
  24. }
  25.  
  26. std::vector<std::string> findAddressesFromOrgs_1() {
  27. return findMatchingAddresses(
  28. [](const std::string &addr) { return addr.find("noodles") != std::string::npos; }
  29. );
  30. }
  31.  
  32. std::vector<std::string> findUserString() {
  33. std::string name;
  34. std::getline(std::cin, name);
  35. return findMatchingAddresses(
  36. [&](const std::string &addr) { return addr.find(name) != std::string::npos; }
  37. );
  38. }
  39.  
  40. void insert(std::string &str) { _addresses.push_back(str); }
  41.  
  42. void insertMoveIL(std::initializer_list<std::string> args) {
  43. std::copy(args.begin(), args.end(), std::back_inserter(_addresses));
  44. }
  45.  
  46.  
  47. private:
  48. std::vector<std::string> _addresses;
  49.  
  50. friend std::ostream &operator<<(std::ostream &os, const AddressBook &addressBook) {
  51. for (const auto &x : addressBook._addresses) {
  52. std::cout << x << ' ';
  53. }
  54. std::cout << '\n';
  55. }
  56. };
  57.  
  58. struct CakeType {
  59. size_t _weight;
  60. size_t _value;
  61.  
  62. explicit CakeType(size_t weight = 0, size_t value = 0) : _weight(weight), _value(value) {}
  63. };
  64.  
  65. struct X {
  66. int _x;
  67.  
  68. explicit X(int x) : _x(x) {}
  69. };
  70.  
  71. struct Z {
  72. int _z;
  73.  
  74. explicit Z(int z) : _z(z) {}
  75.  
  76. int operator()(int y) { return _z + y; }
  77. };
  78.  
  79. struct Y : X {
  80. int _y;
  81.  
  82. explicit Y(int x, int y = 0) : X{x}, _y(y) {}
  83. };
  84.  
  85. struct B {
  86. void f(double);
  87. };
  88.  
  89. struct D : B {
  90. using B::f; // bring all f()s from B into scope
  91. void f(int); // add a new f()
  92. };
  93. /* within main()...
  94. * B b; b.f(4.5); // fine
  95. * D d; d.f(4.5); // calls D::f(double) which is B::f(double)
  96. * // without using in using B::f in D, surprise: calls f(int) with argument 4
  97. */
  98.  
  99.  
  100. // Suffix return type syntax
  101. template<typename T, typename U>
  102. auto mult(T x, U y) -> decltype(x * y) { return x * y; }
  103.  
  104.  
  105. int main() {
  106.  
  107. constexpr long long longx = 9223372036854775807LL;
  108. std::cout << std::is_literal_type<long>::value << std::endl;
  109.  
  110. X x(3);
  111. std::cout << x._x << std::endl;
  112.  
  113. Z z(5);
  114. std::cout << z(6) << std::endl; // 11
  115.  
  116. std::cout << mult(6, 7) << std::endl;
  117.  
  118. AddressBook bk{"2", "33"};
  119.  
  120. bk.insertMoveIL({"2", "1", "0", "5"});
  121.  
  122. std::string name("happy");
  123. bk.insert(name);
  124.  
  125.  
  126. std::cout << bk;
  127.  
  128.  
  129. auto results = bk.findUserString();
  130. for (const auto &xresults : results) std::cout << xresults << ' ';
  131.  
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138. // std::cout << std::is_literal_type<long>::value << std::endl;
  139. // Understanding the power of structs. See type_traits in stl for std::is_literal_type.
  140. // I deduced how to write this by checking stl implementation.
  141. // std is namespace, i_literal_type is struct <long> is _Tp template parameter for
  142. // base struct integral_constant which is bool. value is typedef its template parameter _Tp __v
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement