When we called rezise member function of Vector(STL Type) type variable, content from vector remains constant or not?
I am using a vector variable.
std::vector<> mArray开发者_StackOverflow社区;
when i need to resize this variable i called mArray.resize()
function. Does that keep the content of mArray
remains same?
Thanks in advance.
resize()
adds or removes elements, depending on the size you pass to resize()
.
If you just want to allocate space for future elements, use reserve()
instead.
Yes, the elements already in the vector keep their values, at least if you didn't shrink the vector, then the last elements will be lost, of course. The new elements (if you grow the vector) will be standard constructed (or copied from the optional value argument).
As others have said, if you're growing the vector the contents up to the original size will be the same, and if you're shrinking the vector only the contents at the end are lost.
A point to emphasize is that this depends on the copy constructors and operator= of the contained objects being implemented correctly. The resized vector may not contain the original objects, but copies of those objects.
when i need to resize this variable i called mArray.resize() function. Does that keep the content of mArray remains same?
It depends:
1> When the new size is bigger than then current size, yes, the existing content remains the same. New object will be appended to the content using default constructor or value initialization.
2> When the new size is smaller than then current size, the vector will shrink to new size and some content will be lost.
精彩评论