Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int a[7];
- std::cout << "Length of array = " << (sizeof(a)/sizeof(*a)) << std::endl;
- int *p = new int[7];
- std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;
- void func(int *p)
- {
- std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;
- }
- int a[7];
- func(a);
- template<class T, size_t N>
- constexpr size_t size(T (&)[N]) { return N; }
- #include <iostream>
- int main ()
- {
- using namespace std;
- int arr[] = {2, 7, 1, 111};
- auto array_length = end(arr) - begin(arr);
- cout << "Length of array: " << array_length << endl;
- }
- #include <iterator>
- uint32_t data[] = {10, 20, 30, 40};
- auto dataSize = std::size(data);
- // dataSize == 4
- #include <iostream>
- #include <type_traits> // std::remove_extent std::remove_all_extents std::rank std::extent
- template<class T, size_t N>
- constexpr size_t length(T(&)[N]) { return N; }
- template<class T, size_t N>
- constexpr size_t length2(T(&arr)[N]) { return sizeof(arr) / sizeof(*arr); }
- int main()
- {
- int a[5][4][3]{{{1,2,3}, {4,5,6}}, { }, {{7,8,9}}};
- // New way
- constexpr auto l1 = std::extent<decltype(a)>::value; // 5
- constexpr auto l2 = std::extent<decltype(a), 1>::value; // 4
- constexpr auto l3 = std::extent<decltype(a), 2>::value; // 3
- constexpr auto l4 = std::extent<decltype(a), 3>::value; // 0
- // Mixed way
- constexpr auto la = length(a);
- //constexpr auto lpa = length(*a); // compile error
- //auto lpa = length(*a); // get at runtime
- std::remove_extent<decltype(a)>::type pa; // get at compile time
- //std::remove_reference<decltype(*a)>::type pa; // same as above
- constexpr auto lpa = length(pa);
- std::cout << la << ' ' << lpa << 'n';
- // Old way
- constexpr auto la2 = sizeof(a) / sizeof(*a);
- constexpr auto lpa2 = sizeof(*a) / sizeof(**a);
- std::cout << la2 << ' ' << lpa2 << 'n';
- return 0;
- }
- constexpr auto l = sizeof(a) / sizeof(std::remove_all_extents<decltype(a)>::type);
- #include <iostream>
- #include <type_traits>
- template<class T>
- constexpr size_t len(T &a)
- {
- return sizeof(a) / sizeof(typename std::remove_all_extents<T>::type);
- }
- int main()
- {
- int a[5][4][3]{{{1,2,3}, {4,5,6}}, { }, {{7,8,9}}};
- constexpr auto ttt = len(a);
- int i;
- std::cout << ttt << ' ' << len(i) << 'n';
- return 0;
- }
- const std::string s[3] = { "1"s, "2"s, "3"s };
- constexpr auto n = std::extent< decltype(s) >::value; // From <type_traits>
- constexpr auto n2 = std::extent_v< decltype(s) >; // C++17 shorthand
- 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
- constexpr auto size = std::tuple_size_v< decltype(a) >;
- std::cout << n << " " << n2 << " " << size << "n"; // Prints 3 3 3
- int x[2] = {0,1,2};
- #include <array>
- array<type_of_the_array, number_of_elements_in_the_array> Name_of_Array = {};
- Name_of_Array.size();
- #include<iostream>
- #include<array>
- int main()
- {
- std::array<int,3> arr;
- //To find the size of the array
- std::cout<<arr.size()<<std::endl;
- //Accessing the last element
- auto it=arr.end();
- std::cout<<arr.back()<<"t"<<arr[arr.size()-1]<<"t"<<*(--it);
- return 0;
- }
- #define GOOGLE_ARRAYSIZE(a)
- ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
- // test codes...
- char* ptr[] = { "you", "are", "here" };
- int testarr[] = {1, 2, 3, 4};
- cout << GOOGLE_ARRAYSIZE(testarr) << endl;
- cout << GOOGLE_ARRAYSIZE(ptr) << endl;
- string myArray[] = { "Example1", "Example2", "Example3", "Example4" };
- int size_of_array=size(myArray);
- >>> 4
- template <typename T,unsigned S>
- inline unsigned arraysize(const T (&v)[S]) { return S; }
- length = sizeof(array_name)/sizeof(int);
- int prime[] = {0};
- int primes(int x, int y){
- using namespace std; int a = 1;
- for (int i = x; i <= y; i++){prime[a] = i; a++; }
- prime[0] = a; return 0;
- }
- class MyObject;
- struct MyObjectList
- {
- std::list<MyObject> objects;
- MyObjectList& operator<<( const MyObject o )
- {
- objects.push_back( o );
- return *this;
- }
- };
- someFunc( MyObjectList &objects );
- someFunc( MyObjectList() << MyObject(1) << MyObject(2) << MyObject(3) );
- template <class T, size_t N>
- char (&helper(T (&)[N]))[N];
- #define arraysize(array) (sizeof(helper(array)))
- int main() {
- int a[10];
- std::cout << arraysize(a) << std::endl;
- return 0;
- }
- unsigned int x[] -> int x[]
- // malloc/new
- arr[0] = length;
- arr++;
- // do anything.
- int len = *(arr-1);
- free(--arr);
- int global[] = { 1, 2, 3, 4 };
- sizeof(global) / 4;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement