lundi 23 septembre 2019

Can non type template arguments done on stl containers?

template<typename T,int nSize>
T Sum(T (&parr)[nSize])
{
    T sum=0;
    for(int i = 0; i < nSize ;++i)
    {
        sum += parr[i];
    }
    return sum;
}
int _tmain(int argc, _TCHAR* argv[])
{
    int nArr[] = {1,2,3,4};
    int nSum = Sum(nArr);
   std::cout<<"Sum :"<<nSum;
}

Can std::vector be used instead of array.Or can array be replaced by any of the stl containers?

Aucun commentaire:

Enregistrer un commentaire