4

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 :)

Maroun
  • 94,125
  • 30
  • 188
  • 241
younis
  • 147
  • 2
  • 6
  • What do you mean by not knowing the data? You can use variables on the initializer list (inside the `{}` braces). If you don't know the size of the array at the time of writing code, you should use dynamically allocated arrays (which of course cannot be initialized this way). – matb Nov 03 '15 at 09:07
  • 1
    Oh, you are really right. I meant that I know the max size of the array. Yes, since there are some may be not initialized it is a pity cannot initialize the front part. – younis Nov 03 '15 at 09:17

3 Answers3

4

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.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
3

Indeed, you cannot dynamically read data into a const array.

You can--however--provide a const interface to mutable data, via a const reference or pointer.

So long as you are asking good questions about constness you might go ahead and make the leap to std::array...

#include <array>
#include <algorithm>
#include <iostream>

int main() {
    std::array<int, 5> writable = {4,3,0,1,2};
    const std::array<int, 5> & readable = writable;

    // you can mutate the writable (and use algorithms...)
    std::sort(writable.begin(), writable.end());

    // readable[0] = 1020; // this would give an error

    // but read access is fine
    for(int i : readable)
        std::cout << i << ' ';
}

(Note: As you say you "don't know what your data is", there are different degrees of not knowing. There's not knowing how many there are, or knowing how many there are but just not the values. I actually deal with enough situations of knowing-how-many-and-not-the-values that I don't assume such things do not happen, buuuut... other people are pointing out the likely possibility you don't know how many there are either. In which case, use a vector!)

Community
  • 1
  • 1
  • I mean, if you already use `std::array` you can also just initialise the `const` array directly from a function call result — `std::array<…> const variable = function();` – Konrad Rudolph Nov 03 '15 at 09:16
  • @KonradRudolph But then you write the function, and then you name the function, and then you write the documentation for the function, and then you sell the rights to Hollywood to make the movie. Where does it end? Everyone can take N steps away from the code given and second guess... I assume people can ask the next question if they don't know how to put the principle to practice. – HostileFork says dont trust SE Nov 03 '15 at 09:22
  • Wow, slippery slope done right. Props, I guess. :) (Honestly, it’s a very bad argument, there’s overwhelming benefit to writing a small function here, but it made me smile.) – Konrad Rudolph Nov 03 '15 at 09:27
  • Damn. I forgot to secure a deal for the movie rights to my answer then – sehe Nov 03 '15 at 10:12
2

You can somewhat read into a const array:

const auto a = []{
    std::array<int, 5> r;
    std::cout << "Enter " << r.size() << " integers: ";
    std::copy_n(std::istream_iterator<int>(std::cin), r.size(), r.begin());

    return r;
}();

Live On Coliru

int main() { return std::accumulate(a.begin(), a.end(), 0u); }

Program returns the sum of the integers read.

sehe
  • 374,641
  • 47
  • 450
  • 633