vendredi 6 septembre 2019

Why do std::begin() and std::end() work with fixed arrays, but not with dynamic arrays?

I can't understand why the std::begin() function doesn't work when it is given an int * arr pointer, but it works with an int arr[] array.

This code doesn't work:

int *arr = new int[5]{ 1,2,3,4,5 };

if (find(begin(arr),end(arr),5)!=end(arr))
{
    cout << "found";
}

This code does work:

int arr2[5] = { 1,2,3,4,5 };

if (find(begin(arr2),end(arr2),5)!=end(arr2))
{
    cout << "found";
}

Aucun commentaire:

Enregistrer un commentaire