Re: Newbie Question: 'Sizeof' - I'm unclear what it actually measures, Modern equiv of older style reference manual?
Christian Hackl <hacki@sbox.tugraz.at> wrote:
Juha Nieminen ha scritto:
Christian Hackl <hacki@sbox.tugraz.at> wrote:
In C++, the correct way to create an array of 5 ints and print its size
is as follows:
[...]
std::vector<int> scores;
Says who?
If I say that the "correct" way of create such an array is by using
std::deque, are you going to say that I'm wrong?
Yes, because it goes against experience and common sense.
Exactly how does it go against "experience and common sense"?
An array is a sequential data container which can be indexed in constant
time. Both std::vector and std::deque fulfill that requirement. You can
traverse an array in linear time, and both std::vector and std::deque
fulfill that requirement. If we are talking about dynamic arrays, where
you can add and remove elements at will, both std::vector and std::deque
fulfill that requirement. They even have the same computational complexity
for doing so (at least when appending data to the end or insertinging data
to the middle). They even have similar iterator invalidation behavior.
The one major difference between the two is that you can insert elements
to the beginning of a std::deque in constant time, while with std::vector
that would be a linear-time operation. However, that hardly disqualifies
std::deque as a valid substitute for raw arrays.
They look pretty much similar on their public interfaces (even if their
internal implementation is quite different, but that should be pretty
inconsequential in most cases).
So, once again: Why exactly is std::vector the "correct" way to create
an array and std::deque the "wrong" way?
Due to their internal implementation there are situations where
std::vector is more suitable and efficient, while in other situations
std::deque is considerably better (either because of its additional
features, its efficiency in certain situations, or both). However,
as a pure array replacement they look pretty much the same.
std::vector is clearly designed as an array replacement.
As said, std::deque looks pretty much the same from the outside.
What stops it from being a good array replacement?
There's a reason no one starts teaching STL containers with std::deque.
And what would that reason be? Prejudice?
Realistically, the next thing he will wish to learn is how to read some
integer numbers from the command line, and at this point he already has
to unlearn everything he was taught about efficient fixed-size arrays.
Has to unlearn? That doesn't make any sense. You talk like a person
could only know either about fixed arrays or about std::vector, but not
both at the same time, and thus if he wants to learn about std::vector
he has to "unlearn" fixed arrays.
Obviously a person, even a beginner, can understand *both* constructs
at the same time. How about teaching both at the same time?