Find array size C++

An array is a container having elements of the same data type. If we don’t know the actual size of an array, it can be determined by different methods. When we talk about the size of an array, in actual we are talking about the number of elements present in the array. Sometimes, we define the array size, and sometimes the brackets are left empty. This is an apparent size that only shows the capacity of an array to store value in it. For instance, consider a sample array declaration
Int array[] = {1,2,3,4,5,6}

Here the size of an array or the length of an array is 6. And total array size that is to be assigned is not shown. The actual size is obtained by applying different operations. These operations are used in this article to obtain the size of an array.

Example 1

In this illustration, we will utilize the concept of begin() and end(). Through this method, the size of an array can be known easily. These are two libraries that are known for standard libraries. These two functions return the iterators that show the preliminary and ending points of the array. Starting from the header, we use an array library. This will include all the functions related to the array. In the main function, we have initiated an array having integer values.

Cout<< ……….”<<end(a)-begib(a)<<

Here we don’t have mentioned array size. In the display statement following the cout, we have use end() and begin() functions. The dissimilarity between these two functions will show us the size of an array. In the parameters of these functions, we have passed the array. By doing this, the actual size will be determined. The resultant value from these functions is directly displayed.

Now moving towards the output. We are supposed to execute these programs in Linux, so we need the involvement of the Ubuntu terminal. As we are using C++ code, so we need to compile the code through the compiler. That is the G++ compiler. After compiling the code, we will execute it. The below commands show the output approach we have used.

$ g++ -o code2 code2.c

$ ./code2

You can now see the output. Another similar example in the case of std is the distance function. In this distance is calculated by using begin() and end() functions. This is completed by using these functions with std.

Int n= std:: distance(std::begin(arr), std::end(arr));

The output is obtained in the cout statement. To see the record, use the compiler again to execute the code.

Here you can see that our desired output is obtained.

Example 2

This example relates to the usage of the “sizeof()” function in the C++ code, as this value returns the actual size of data in the form of bytes. Furthermore, it also deals with returning the number of bytes that are used to store an array. In other words, In this example, the first step is initializing an array without declaring the size of an array. the syntax used for the sizeof() function is :

Int al = sizeof(arr)/sizeof(arr[0]);

Where arr is the array. arr[0] shows the index of the elements in the array.

So this statement implies that the array size is divided by the size of all the elements present, one by one. This helps in the calculation of the length. We have used an integer variable to receive and store the value return from the function.

We will get the output here from the command prompt by the same compile-execution method.

The output shows the size of the array, which implies the number of elements present in it, which is 6.

Example 3

This example includes the usage of the size () function. This function is placed in the standard library, STL. The initial step in the main program is the array declaration. Here the name of the array also contains the size and integer value. This method also returns the result directly in the output statement.

Cout<<….<<arr.size()<<

Where ‘arr’ is the array, to fetch the result or access the function, we need the array’s name with the size function.

To display the result, we use the g++ compiler to compile and execute the result.

From the output, you can see that the result is our desired one that shows the actual size of the array.

Example 4

The size of an array can also be obtained by using pointers as pointers store the address/location of the variable’s value. Now consider the below given an example.

The initial step is to initialize an array as usual. Then the pointer works for the array size.

Int len = *(&array + 1) – array;

This is the core statement that works as a pointer. “*” is used to locate the position of any element in an array, whereas the “&” operator is used to obtain the value of the location obtained through the pointer. This is the way we obtain array size from pointers. The resultant is shown through the terminal. The answer is the same. As the size of the mentioned array was stated as 13.

Example 5

In this example, we have used the idea of template argument deduction. A template argument is a parameter of a special kind. It is used to pass an argument of any type, just like the regular functions that can be passed as an argument.

When an array is passed as a parameter, it is converted into a pointer to show the address. To obtain the length of the specific array, we use this approach of template argument deduction. Std is a short form of standard.

Considering the given an example, we have introduced a template class used to obtain the array size. It is a default built-in class that contains all the functionalities of template arguments.

Constexpr std : : size_t size(const T (&array)[N]) noexcept {

return N;

}

This is a constant line in this concept. The output is obtained directly in the cout statement.

From the output, you can see that we have got our desired output: the array size.

Example 6

We use std:: vector to obtain the array size in the program. This is a type of container; its function is to store dynamic arrays. It works with different methods for different operations. To accomplish this example, we have used a vector library that includes all the vector functions in it. It also declares the cin, cout, endl, and vector statements to be used in the program. An array is initiated first in the program. The output is displayed in the cout statement by vector size.

Cout<< “vector size:<<int_array.size() << endl;

Now we will see the output from the Ubuntu terminal. The size of the array is accurate to the elements present in it.

Conclusion

In this tutorial, we have used a different approach to obtain the array length or size. Some are built-in functions, whereas others are used manually.



from Linux Hint https://ift.tt/3x6kevG

Post a Comment

0 Comments