Gets an array of length

Non-string array

#include<iostream>
using namespace std;

template<class T>

int length(T& arr)
{
    return sizeof(arr) / sizeof(arr[0]);
}

int main()
{
    int arr[] = { 1,5,9,10,9,2 };
    // 方法一
    cout << "数组的长度为:" << length(arr) << endl;
    // 方法二
    cout << "数组的长度为:" << end(arr)-begin(arr) << endl;
    return 0;
}

A method must use the function, otherwise it is impossible to become an array int [6] type from int * type.

Array of strings

String array, directly strlen () function.

Published 52 original articles · won praise 0 · Views 683

Guess you like

Origin blog.csdn.net/UniversityGrass/article/details/104680758