void array_print(vector<int> & arr) { for (int i = 0; i < arr.size(); ++i) cout<<i<<" Ячейка массива:"<<arr[i]<<endl; } void array_print(int arr[]) { for (int i = 0; i < /*arr.size()*/; ++i) cout<<i<<" Ячейка массива:"<<arr[i]<<endl; } 
  • one
    function from standard library std :: size . - acade
  • Yes, but only for objects of type "array". You have the same function in the array_print parameter arr is a pointer to an array element. It is impossible to know the size of the array by the pointer to its element. - AnT

2 answers 2

In the array_print function array_print the arr parameter is a pointer to an array element. It is impossible to know the size of the array by the pointer to its element.

In a situation where the size of the array is not fixed at the compilation stage, you have only one option - to transfer the size of the array from the outside manually

 void array_print(int arr[], int n) { for (int i = 0; i < n; ++i) cout<<i<<" Ячейка массива:"<<arr[i]<<endl; } 

    There is a sample.

     template<class T, size_t N> size_t length(T(&)[N]) { return N; } int main() { int a[10]; cout << length(a) << endl; } 

    But note that the focus type

     int * a = new int[20]; cout << length(a) << endl; 

    will not pass.

    How not to pass

     void func(int a[]) { cout << length(a) << endl; } 
    • I need to use the length () value in the function - CHilll
    • one
      Pass it to the function as a separate parameter ... - Harry