Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read data to construct a const array?

const variables can't be modified after declaration. I know I can define a const int by:

const int a[5]={0,1,2,3,4};

But the problem is that I don't know exactly what the data is. And reading the data only once is enough, and can I store these in a const array by some pointer operations? I will appreciate it if you may give some hints :)

like image 414
younis Avatar asked Jan 07 '23 06:01

younis


1 Answers

Use a function:

std::vector<int> readArray()
{
    std::vector<int> array;
    // populate array
    return array;
}

Then:

const std::vector<int> constArray = readArray();

Replace std::vector with std::array if you know the number of elements beforehand.

like image 60
emlai Avatar answered Jan 26 '23 05:01

emlai