fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. template <typename T, size_t sizet>
  9. size_t size_t_fnof(T (&array)[sizet]);
  10. #define setting_size_t(z) z,size_t_fnof(z)
  11.  
  12. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  13.  
  14. #define element_of_array 10
  15.  
  16. #define array_and_sizet int array[], size_t sizet
  17.  
  18. int find_max_fn(array_and_sizet);
  19. int find_max_element_fn(array_and_sizet);
  20. /*
  21. template<size_t sizet>
  22. int find_max_fn_using_with_std__array(array<int,sizet>array);
  23. template<size_t sizet>
  24. int find_max_element_fn_using_with_std__array(array<int,sizet>array);
  25. */
  26. #define nameoftemplate template<size_t sizet>
  27. #define fnnameofarray(type,name,array) type name(array<int,sizet>array)
  28. #define templatefnarray(type,name,array) nameoftemplate fnnameofarray(type,name,array)
  29. /*
  30. nameoftemplate
  31. fnnameofarray(int,find_max_fn_using_with_std__array,array);
  32. nameoftemplate
  33. fnnameofarray(int,find_max_element_fn_using_with_std__array,array);
  34. */
  35.  
  36. templatefnarray(int,find_max_fn_using_with_std__array,array);
  37. templatefnarray(int,find_max_element_fn_using_with_std__array,array);
  38. /*
  39. template<typename T, typename A>//allocator generic template
  40. int find_max_fn_using_with_vector(vector<T,A>myvector);
  41. */
  42.  
  43. int find_max_fn_using_with_vector(vector<int>myvector);
  44. int find_max_element_fn_using_with_vector(vector<int>myvector);
  45.  
  46. int main(int argc, char** argv) {
  47. // const size_t element_of_array = 10;
  48. int myarray[element_of_array];
  49. auto value = 0;
  50. array<int,element_of_array>array;
  51. vector<int>myvector(element_of_array);
  52.  
  53. auto scan = [&value](int& variable)->void
  54. {
  55. variable = value++;
  56. return;
  57. };
  58. auto print = [](const int& variable)->void
  59. {
  60. cout<<variable<<endl;
  61. return;
  62. };
  63. cout<<"List all elements in the array"<<endl;
  64.  
  65.  
  66. for(int k=0;k<element_of_array;k++)myarray[k] = k;
  67. for(int k=0;k<element_of_array;k++)cout<<myarray[k]<<endl;
  68. cout<<"find max using with max function: "<<find_max_fn(setting_size_t(myarray))<<endl;
  69. cout<<"find max using with max_element function: "<<find_max_element_fn(setting_size_t(myarray))<<endl;
  70.  
  71. /*
  72. for(auto it = array.begin(); it != array.end(); ++it)*it = value++;
  73. for(auto it = array.begin(); it != array.end(); ++it)cout<<*it<<endl;
  74. */
  75. /*
  76. for_each(array.begin(),array.end(),scan); //set
  77. for_each(array.begin(),array.end(),print); //get
  78. cout<<"find max using with max function: "<<find_max_fn_using_with_std__array(array)<<"(std::array)"<<endl;
  79. cout<<"find max using with max_element function: "<<find_max_element_fn_using_with_std__array(array)<<"(std::array)"<<endl;
  80. */
  81. /*
  82. for(vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)*it = value++;
  83. for(vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)cout<<*it<<endl;
  84. cout<<"find max using with max function: "<<find_max_fn_using_with_vector(myvector)<<"(vector)"<<endl;
  85. cout<<"find max using with max_element function: "<<find_max_element_fn_using_with_vector(myvector)<<"(vector)"<<endl;
  86. */
  87.  
  88.  
  89.  
  90. system("pause");
  91. return 0;
  92. }
  93. template <typename T, size_t sizet>
  94. size_t size_t_fnof(T (&array)[sizet])
  95. {
  96. return sizet;
  97. }
  98. int find_max_fn(array_and_sizet)
  99. {
  100. int largest;
  101. int range = sizet - 1;
  102. for(int k=0;k<range;k++)
  103. if(k==0)
  104. {
  105. largest = max(array[k],array[k+1]);
  106. }
  107. else
  108. {
  109. largest = max(largest,array[k+1]);
  110. }
  111. return largest;
  112. }
  113. int find_max_element_fn(array_and_sizet)
  114. {
  115. int largest;
  116. largest = *max_element(array,array+sizet);
  117. return largest;
  118. }
  119. templatefnarray(int,find_max_fn_using_with_std__array,array)
  120. {
  121. int largest;
  122. int range = sizet - 1;
  123. for(int k=0;k<range;k++)
  124. if(k==0)
  125. {
  126. largest = max(array[k],array[k+1]);
  127. }
  128. else
  129. {
  130. largest = max(largest,array[k+1]);
  131. }
  132. return largest;
  133. }
  134. templatefnarray(int,find_max_element_fn_using_with_std__array,array)
  135. {
  136. int largest;
  137. /*largest = *max_element(array,array+sizet);*/
  138. largest = *max_element(array.begin(),array.end());
  139. return largest;
  140. }
  141. int find_max_fn_using_with_vector(vector<int>myvector)
  142. {
  143. int largest;
  144. size_t sizet = myvector.size();
  145. int range = sizet - 1;
  146. for(int k=0;k<range;k++)
  147. if(k==0)
  148. {
  149. largest = max(myvector[k],myvector[k+1]);
  150. }
  151. else
  152. {
  153. largest = max(largest,myvector[k+1]);
  154. }
  155. return largest;
  156. }
  157. int find_max_element_fn_using_with_vector(vector<int>myvector)
  158. {
  159. int largest;
  160. /*largest = *max_element(array,array+sizet);*/
  161. largest = *max_element(myvector.begin(),myvector.end());
  162. return largest;
  163. }
  164.  
  165.  
Success #stdin #stdout #stderr 0.01s 5512KB
stdin
Standard input is empty
stdout
List all elements in the array
0
1
2
3
4
5
6
7
8
9
find max using with max function: 9
find max using with max_element function: 9
stderr
sh: 1: pause: not found