Jump to content

is there a function that I can use to get thier size of length !?

Like int x=size(arr)

It depends on container, that you are using and if you are using C++11. (Also, sizes should not be stored in int, but in size_t). Examples:

C++11:

std::array<int, 3> arr = { 1, 2, 3 };size_t s_a = arr.size(); // 3, okstd::vector<int> vec = { 1, 2, 3 };size_t s_v = vec.size(); // 3, ok
Before C++11 size() still worked for some STL containers, but not for arrays. But you still can use sizeof keyword in some cases (but be careful, it's easy to make a mistake).

C++:

int arr[] = { 1, 2, 3 };size_t n = sizeof(arr) / sizeof(arr[0])); // 3, okstd::vector<int> vec = { 1, 2, 3, 4 };size_t s_v = vec.size(); // 4, okint *arr2 = new int[7];                     // no standard way to obtain array size; it depends on architecture and compilersize_t int_ptr_size = sizeof(arr2);         // 4 or 8, depending if you compiled for 32bit or 64bit processorsize_t int_size = sizeof(arr2[0]);          // 4 on most architecturessize_t int_size = sizeof(*arr2);            // 4, it's the same as line abovesize_t n = sizeof(arr2) / sizeof(arr2[0])); // 1 or 2

| ← Ceci n'est pas une pipe

Link to comment
https://linustechtips.com/topic/278547-array-vector-size-c/#findComment-3786791
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×