Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. int a[7];
  2. std::cout << "Length of array = " << (sizeof(a)/sizeof(*a)) << std::endl;
  3.  
  4. int *p = new int[7];
  5. std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;
  6.  
  7. void func(int *p)
  8. {
  9. std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;
  10. }
  11.  
  12. int a[7];
  13. func(a);
  14.  
  15. template<class T, size_t N>
  16. constexpr size_t size(T (&)[N]) { return N; }
  17.  
  18. #include <iostream>
  19.  
  20. int main ()
  21. {
  22. using namespace std;
  23. int arr[] = {2, 7, 1, 111};
  24. auto array_length = end(arr) - begin(arr);
  25. cout << "Length of array: " << array_length << endl;
  26. }
  27.  
  28. #include <iterator>
  29.  
  30. uint32_t data[] = {10, 20, 30, 40};
  31. auto dataSize = std::size(data);
  32. // dataSize == 4
  33.  
  34. #include <iostream>
  35. #include <type_traits> // std::remove_extent std::remove_all_extents std::rank std::extent
  36.  
  37. template<class T, size_t N>
  38. constexpr size_t length(T(&)[N]) { return N; }
  39.  
  40. template<class T, size_t N>
  41. constexpr size_t length2(T(&arr)[N]) { return sizeof(arr) / sizeof(*arr); }
  42.  
  43. int main()
  44. {
  45. int a[5][4][3]{{{1,2,3}, {4,5,6}}, { }, {{7,8,9}}};
  46.  
  47. // New way
  48. constexpr auto l1 = std::extent<decltype(a)>::value; // 5
  49. constexpr auto l2 = std::extent<decltype(a), 1>::value; // 4
  50. constexpr auto l3 = std::extent<decltype(a), 2>::value; // 3
  51. constexpr auto l4 = std::extent<decltype(a), 3>::value; // 0
  52.  
  53. // Mixed way
  54. constexpr auto la = length(a);
  55. //constexpr auto lpa = length(*a); // compile error
  56. //auto lpa = length(*a); // get at runtime
  57. std::remove_extent<decltype(a)>::type pa; // get at compile time
  58. //std::remove_reference<decltype(*a)>::type pa; // same as above
  59. constexpr auto lpa = length(pa);
  60. std::cout << la << ' ' << lpa << 'n';
  61.  
  62. // Old way
  63. constexpr auto la2 = sizeof(a) / sizeof(*a);
  64. constexpr auto lpa2 = sizeof(*a) / sizeof(**a);
  65. std::cout << la2 << ' ' << lpa2 << 'n';
  66.  
  67. return 0;
  68. }
  69.  
  70. constexpr auto l = sizeof(a) / sizeof(std::remove_all_extents<decltype(a)>::type);
  71.  
  72. #include <iostream>
  73. #include <type_traits>

  74.  
  75. template<class T>
  76. constexpr size_t len(T &a)
  77. {
  78. return sizeof(a) / sizeof(typename std::remove_all_extents<T>::type);
  79. }
  80.  
  81. int main()
  82. {
  83. int a[5][4][3]{{{1,2,3}, {4,5,6}}, { }, {{7,8,9}}};
  84. constexpr auto ttt = len(a);
  85. int i;
  86. std::cout << ttt << ' ' << len(i) << 'n';

  87. return 0;
  88. }
  89.  
  90. const std::string s[3] = { "1"s, "2"s, "3"s };
  91. constexpr auto n = std::extent< decltype(s) >::value; // From <type_traits>
  92. constexpr auto n2 = std::extent_v< decltype(s) >; // C++17 shorthand
  93.  
  94. const auto a = std::array{ "1"s, "2"s, "3"s }; // C++17 class template arg deduction -- http://en.cppreference.com/w/cpp/language/class_template_argument_deduction
  95. constexpr auto size = std::tuple_size_v< decltype(a) >;
  96.  
  97. std::cout << n << " " << n2 << " " << size << "n"; // Prints 3 3 3
  98.  
  99. int x[2] = {0,1,2};
  100.  
  101. #include <array>
  102. array<type_of_the_array, number_of_elements_in_the_array> Name_of_Array = {};
  103.  
  104. Name_of_Array.size();
  105.  
  106. #include<iostream>
  107. #include<array>
  108. int main()
  109. {
  110. std::array<int,3> arr;
  111.  
  112. //To find the size of the array
  113. std::cout<<arr.size()<<std::endl;
  114.  
  115. //Accessing the last element
  116. auto it=arr.end();
  117. std::cout<<arr.back()<<"t"<<arr[arr.size()-1]<<"t"<<*(--it);
  118.  
  119. return 0;
  120. }
  121.  
  122. #define GOOGLE_ARRAYSIZE(a)
  123. ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
  124.  
  125. // test codes...
  126. char* ptr[] = { "you", "are", "here" };
  127. int testarr[] = {1, 2, 3, 4};
  128. cout << GOOGLE_ARRAYSIZE(testarr) << endl;
  129. cout << GOOGLE_ARRAYSIZE(ptr) << endl;
  130.  
  131. string myArray[] = { "Example1", "Example2", "Example3", "Example4" };
  132. int size_of_array=size(myArray);
  133.  
  134. >>> 4
  135.  
  136. template <typename T,unsigned S>
  137. inline unsigned arraysize(const T (&v)[S]) { return S; }
  138.  
  139. length = sizeof(array_name)/sizeof(int);
  140.  
  141. int prime[] = {0};
  142. int primes(int x, int y){
  143. using namespace std; int a = 1;
  144. for (int i = x; i <= y; i++){prime[a] = i; a++; }
  145. prime[0] = a; return 0;
  146. }
  147.  
  148. class MyObject;
  149.  
  150. struct MyObjectList
  151. {
  152. std::list<MyObject> objects;
  153. MyObjectList& operator<<( const MyObject o )
  154. {
  155. objects.push_back( o );
  156. return *this;
  157. }
  158. };
  159.  
  160. someFunc( MyObjectList &objects );
  161.  
  162. someFunc( MyObjectList() << MyObject(1) << MyObject(2) << MyObject(3) );
  163.  
  164. template <class T, size_t N>
  165. char (&helper(T (&)[N]))[N];
  166.  
  167. #define arraysize(array) (sizeof(helper(array)))
  168.  
  169. int main() {
  170. int a[10];
  171. std::cout << arraysize(a) << std::endl;
  172. return 0;
  173. }
  174.  
  175. unsigned int x[] -> int x[]
  176.  
  177. // malloc/new
  178.  
  179. arr[0] = length;
  180. arr++;
  181.  
  182. // do anything.
  183. int len = *(arr-1);
  184.  
  185. free(--arr);
  186.  
  187. int global[] = { 1, 2, 3, 4 };
  188.  
  189. sizeof(global) / 4;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement