J
Juha Nieminen
Sam said: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.
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.
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 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.)
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.)