Paul <
peps...@gmail.com> wrote:
> The quote below is from a book
> about algorithms in C++, and is discussing representations of graphs.
> I don't understand what he means by initializing a vector with small
> capacity. The nearest thing that I'm aware of is the reserve()
> method. However, that only works to request more capacity, not to
> limit capacity. I'd be grateful for any explanations. The ways that
> I'm aware of, for initializing a vector, don't involve the capacity.
If you are looking for extreme space efficiency in such situations, you
may well have to create your own vector substitute.
If you know for certain that all the vectors will have a certain maximum
size, and this size is relatively small, just use std::array.
If the vast majority of the vectors are small, but you need to support
larger vectors as well, and you need extreme efficiency when dealing
with those small vectors, create your own vector implementation that
uses "short vector optimization" (similar to "short string optimization").
In other words, in your vector class theres some member array of
the size you deem appropriate, and whenever there's at most that many
elements in the vector, use it. If the amount of elements exceeds that
amount, use the array for the data needed to handle the elements as
a dynamic array (this can be directly done with a union.)
If the large vectors are very often copied/assigned, but seldom modified,
and restricting your vector to move semantics doesn't suffice, you could
in addition use copy-on-write semantics.
This becomes a bit complicated implementation-wise, but when well
done, it will make your program more efficient.
--- news://
freenews.netfront.net/ - complaints:
ne...@netfront.net ---