2

I've been reading Accelerated C++ 2000 for a few days now, and I came upon the vector<~>.

Suppose I want to append values to my vector that must never change, should I write

const vector<double>;

or

vector<const double>;

?

I'm asking because I'm not sure which one will work according to what I want to happen.

Nogurenn
  • 858
  • 3
  • 11
  • 28
  • 1
    Note that you cannot delete elements from a vector of const elements, because deleting the element at index i requires assigning to all elements at index >= i. – fredoverflow Sep 21 '13 at 08:55

1 Answers1

3

This cannot be done. The component type of vectors must be asssignable. That means you cannot append to vector values that cannot be changed. Why can't I make a vector of references?

All you can to is to make vector const:

const vector<double>;

But this means that the vector cannot be changed. You cannot add to it nor change its elements.

Community
  • 1
  • 1
cpp
  • 3,743
  • 3
  • 24
  • 38
  • Just a clarification. We're allowed to prepend a **const** inside the angle brackets because C++ syntax dictates that the data type of the contents vector<~> has should be declared inside the angle brackets? By that, I mean it's not the usual "dataType name" declaration. – Nogurenn Sep 21 '13 at 05:05
  • 1
    @solitude Yes. Google "C++ templates". –  Sep 21 '13 at 05:06
  • Yes, in this case the data type is `const double` – cpp Sep 21 '13 at 05:07
  • 2
    `vector` won't compile for me. Should it? I get what I call "template vomit." – Fred Larson Sep 21 '13 at 14:08
  • @FredLarson What does a "template vomit" mean? – Nogurenn Sep 24 '13 at 00:18
  • @solitude: It's my own term for the frightening cascade of error messages you get from errors using templates. – Fred Larson Sep 24 '13 at 04:09