arkoenig 340 Practically a Master Poster

Without reading the whole file, you cannot select every line with equal probability. One reason is that you have no way of knowing whether the portions of the file you did not read consist of one long line each, or a whole bunch of little tiny lines each.

arkoenig 340 Practically a Master Poster

@jkoske: You know what the question is. It's "I've done a little bit of my homework; will you do the rest for me?" The answer is (or should be) no.

arkoenig 340 Practically a Master Poster

Otherwise, we wind up with the original poster writing

myVec.clear();
VecType vert;
myVec.swap(vert);

without understanding why the call to clear is unnecessary, or why the second and third statements could have been written as

VecType().swap(myVec);      // This works...

but not as

myVec.swap(VecType());      // ...but this doesn't.

It occurred to me just now that these remarks don't tell the whole story. Consider:

// Example 1
{
    VecType myVec;

    // Put a bunch of stuff in myVec

    VecType().swap(myVec);      // Clear myVec

    // Put a bunch of stuff in myVec again

    ...
}

// Example 2
{
    VecType myVec;

    // Put a bunch of stuff in myVec

    VecType temp;
    temp.swap(myVec);      // Clear myVec

    // Put a bunch of stuff in myVec again

    ...
}

These two examples are likely to have very different characteristics in terms of how much memory they consume. For the moment, I'll leave it as an exercise to figure out why. When you've figured it out, I think you will see why I say that there is more to this problem of controlling memory allocation than meets the eye, and perhaps you will understand better why I wanted to concentrate on the fundamentals first.

I am reminded of Brian Kernighan's two rules of optimization.

1) Don't do it.

2) [for experts only] Don't do it yet.

arkoenig 340 Practically a Master Poster

Failing to solve the problem by oversimplifying is not the same thing as premature optimization.

Failing to understand a problem thoroughly before trying to solve it is the kind of carelessness that often leads to premature optimization.

Evidently we have different ideas about what is important. Nothing wrong with that--but I would like to suggest that you think twice before assuming that everyone who disagrees with you is wrong.