Re: about new and delete
Sam wrote:
std::list uses more memory and more allocations than std::vector does.
Very funny. You're a real comedian, you should be writing for SNL.
You've got your blinders on, and can't see all the additional memory
std::vector allocates, in order to reserve room for the growth of a
vector. There are plenty of situations where std::vector uses more
memory than std::list would, with the same number of elements.
Your blanket, absolute statement is evidence that you do not fully
understand the subject matter at hand.
Unlike you, I actually *measure* things, rather than relying to
theoretical handwaving.
For example, I measured the memory usage of a program which does this:
std::vector<int> v;
for(int i = 0; i < 1000000; ++i) v.push_back(i);
The amount of memory taken by the program was 4.77 MB. Then I changed
the std::vector to std::list and ran the test again. The memory taken by
the program was 16 MB.
You are referring to indexing a vector to access its elements. That only
matters when random access is a requirement. It is not always required,
and not in this sample case.
And then you try to argue how std::list is so much better because you
can insert in the middle in constant time, as if that had been a
requirement in this case.
I am merely pointing out situations where std::list is a better choice,
in response to illogical claims about the superiority of std::vector in
all possible situations.
Exactly who claimed that std::vector is superior in all possible
situations? The only thing that I, or anyone else I have seen, have
claimed is that std::vector is superior in *this* particular situation.
You didn't even disagree that iterating through the elements is
simpler with std::vector than it is with std::list, yet you refuse to
I most certainly disagreed. You're in error.
Well, as I already mentioned in another post, I don't believe you
really think that traversing a vector is not simpler than traversing a
list, but you are only claiming it for the sake of argument and because
you can't retreat from your position anymore.
Using iterators might be more *generic*, but that's not the same thing
as being *simpler*. Those are two radically different things. Your only
argument pro using iterators to traverse the container is that it's more
generic.
In this particular case the original poster will benefit from
simplicity more than from genericity.
You're going to have to consider
all of std::vector's baggage, such as excessive memory usage, excessive
copying, and iterator invalidation in analogous situations where a
std::list would not invalidate any of the existing iterators.
You are going to have to consider all the std::list's baggage, such as
excessive memory usage, slow allocation of a high amount of elements and
slow random access.
You have me confused with someone else. I am not the one who began the
argument with absolute claims of the kind that I quoted up top.
You started the argument that std::list is some kind of pure win in
this situation. I disagree. Your arguments on why std::list is better
are flawed.
When measuring all possible factors in this particular situation,
std::deque comes out as a clear winner in all counts (including speed,
which was your original argument, and std::deque wins by a huge margin).
However, you didn't even consider std::deque as an alternative. My guess
is that you don't even understand how std::deque works, which is why it
didn't occur to you to suggest using it.
Clearly std::list is *not* the best solution in this particular situation.
(Of course the point is moot anyways. If the task is to ask the user
some numbers and then do something with them, it doesn't really matter
which container you use.)
But, in singing the praises of std::vector, its fan club always fails to
consider these things.
And you fail to consider the drawbacks of std::list.
Wrong, as usual. Unlike other participants of this thread, I never made
an absolute statement that one container always has better results in
all possible situations.
And who might these "other participants" be? I don't remember seeing
anybody claiming that std::vector (or any other container) is best in
*all possible situations*. The only claim has been that std::vector is
better in *this particular situation*. Nobody has said anything about
all possible situations (on the contrary, other hypothetical situations
where std::vector might not be the best have been addresses in this
thread more than once, by other people than just yourself).
I merely pointed out that within the confines
of OP's test case, std::list comes out on top.
Which is clearly a wrong statement. std::deque beats your std::list
hands down in all possible categories ("within the confines of the OP's
test case", and far beyond).
Now, be honest and confess the true reason why you didn't suggest
using std::deque, and instead suggested using std::list. (My guess: The
possibility didn't even cross your mind.)
If the OP was trying to
do something else, such as reading the input data from a file, where the
potential number of elements might be quite large; or if the OP wanted
to sort the elements, for example, that would've called for a different
approach.
Just out of curiosity: Why wouldn't std::list be feasible for sorting
the elements? std::list does have a O(n log n) sorting function. (And if
the elements were very large and heavy to copy, then it could even beat
std::sort applied to a std::vector.)