This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

About std::vector::resize().


Hello,
	Consider:

	std::vector<std::vector<int> > vv;

Now, if this vector were to be resized, then the operation would cause a
lot of memory movement, because vector's copy ctor, and assignment
operator would do a lot of de-allocation, and re-allocation of memory.
However, IMHO this can be prevented by making use of the fact that the
vector can be swapped very easily, in an exception safe manner. So,
instead of actually copying, we may swap an empty vector with the actual
vector to get the desired effect. This would prevent a lot of copying.
So, when the vector grows in size during normal operation such as during
a push_back(), even then this copying can be prevented. So, we may
specialize the functions for the standard containers, so that whenever
we have a std::vector<standard_container>, then the copying while
resizing is avoided totally. However, this does not seem necessary for
std::string, because the implementation is reference counted anyways.

 Same goes for:

std::vector<std::list<int> > lv;

And for the other containers too.

The only remaining thing would be that is this permitted by the
standard? From my partial reading of the 1996 standard, this seems OK,
but there may be something that I overlooked.

-- 
        -Dhruv Matani.
http://www.geocities.com/dhruvbird/

Proud to be a Vegetarian.
http://www.vegetarianstarterkit.com/
http://www.vegkids.com/vegkids/index.html



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]