Guest User

Untitled

a guest
Dec 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1.  
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using std::cout;
  8. using std::endl;
  9. using std::vector;
  10. using std::string;
  11.  
  12. class Person
  13. {
  14. private:
  15. int _age;
  16. string _name;
  17. public:
  18. //1. Prefer default parameters than overloading
  19.  
  20. /*
  21. Person(int age, string name) : _age(age), _name(name){}
  22.  
  23. Person(int age) : _age(age), _name(""){}
  24.  
  25. Person(string name) : _age(1), _name(name){}
  26. */
  27.  
  28. Person(int age=1, string name="") : _age(age), _name(name){}
  29.  
  30. /*
  31.  
  32. void GetOld() {
  33. _age++;
  34. }
  35.  
  36. void GetOld(int n){
  37. _age += n;
  38. }
  39. */
  40.  
  41. void GetOld(int n=1){
  42. _age += n;
  43. }
  44.  
  45. //When you implement in separate cpp, then follow below practices
  46.  
  47. //void Person::GetOld(int n/*=1*/)
  48. //{
  49. // _age += n;
  50. //
  51. //}
  52.  
  53. };
  54.  
  55. int main(int argc, char* argv[])
  56. {
  57. vector<int> vInt(20); //without size when it's initialized, it will be crashed
  58. int i = 0;
  59.  
  60. //vInt must pre-determin its size before it called in begin() and to be filled in generate_n function
  61. // also, size of the container must be matched with the size used in generate_n function.
  62.  
  63. //2. Prefer std::generate to fill in arbitary container as it's more expressive and readable
  64. std::generate_n(begin(vInt), 10, [&i](){
  65. return i++;
  66. });
  67.  
  68. std::generate_n(begin(vInt)+10, 10, [&i](){
  69. return i++;
  70. });
  71.  
  72.  
  73. std::for_each(begin(vInt), end(vInt), [](int element){
  74. cout<<element<<endl;
  75. });
  76.  
  77. //3. Prefer use standard algorithm as it become more powerful with C++11's lambda expression
  78. //all_of
  79. bool areAllItemsLessThan10 = std::all_of(begin(vInt), end(vInt), [](int n){
  80. return (n < 10);
  81. });
  82. cout<<(areAllItemsLessThan10?"Less Than 10" : "Bigger than 10")<<endl;
  83.  
  84. //any_of
  85. bool isAnyOf9 = std::any_of(begin(vInt), end(vInt), [](int n){
  86. return n==9;
  87. });
  88. cout<<(isAnyOf9?"There is 9":"There is no 9")<<endl;
  89.  
  90. //none_of
  91.  
  92. auto biggerThan10 = [](int n){
  93. return n > 10;
  94. };
  95. bool noneOfbiggerThan10 = std::none_of(begin(vInt), end(vInt), biggerThan10);
  96. cout<<(noneOfbiggerThan10?"There is none of bigger than 10":"There is one of bigger than 10")<<endl;
  97.  
  98. //4. with the help of non-member function begin() and end() that introduced in C++11,
  99. // we can get iterator of c-array either.
  100.  
  101. int intArr[] = {0,1,2,3,4,5,6,7,8,9};
  102.  
  103. std::for_each(std::begin(intArr), std::end(intArr), [](int n){
  104. cout<<n<<endl;
  105. });
  106.  
  107.  
  108. return 0;
  109. }
  110.  
  111. /*
  112.  
  113. 0
  114. 1
  115. 2
  116. 3
  117. 4
  118. 5
  119. 6
  120. 7
  121. 8
  122. 9
  123. 10
  124. 11
  125. 12
  126. 13
  127. 14
  128. 15
  129. 16
  130. 17
  131. 18
  132. 19
  133. Bigger than 10
  134. There is 9
  135. There is one of bigger than 10
  136. 0
  137. 1
  138. 2
  139. 3
  140. 4
  141. 5
  142. 6
  143. 7
  144. 8
  145. 9
  146. Press any key to continue . . .
  147.  
  148. */
Add Comment
Please, Sign In to add comment