N
n.torrey.pines
I wonder if resize() is required to check whether a container is
already of the desired size.
already of the desired size.
Yes, the logic specified in the standard isI wonder if resize() is required to check whether a container is
already of the desired size.
if( newSize > size())
extend
else if( newSize < size())
erase
I wonder if resize() is required to check whether a container is
already of the desired size.
I wonder if resize() is required to check whether a container is
already of the desired size.
Only if the new size != size().Andrew said:Resize potentially changes the size, rather than checking.
Exactly what are you trying to accomplish?
Exactly what are you trying to accomplish?
the container is a vector.
I'm up to no good, as usual.
I need to make sure a container is of a certain size. In 99.9% of cases
it already is. So, it makes sense to insert a test if(x.size() !=s) ,
unless it's redundant.
To rephrase the quesion in a language-lawyer-friendly format:
std::vector<int> v;
v.push_back(1);
int* p = &(v[0]);
v.resize(1);
assert(*p == 1); // guaranteed by the standard or not?
if( newSize > size())
extend
else if( newSize < size())
erase
[email protected] said:What does this newSize mean?
For example, ten elements are pushed into a newly created vector. Does
the size mean the numbers of elements in the vector or the memory that
the vector possesses?
Does the size of the vector (memory?) increase automatically to hold all
elements?
I read from another post and it says, erase() used upon vector or other
library utilities doesn't release the memory that is possessed by the
vector.
One people mentioned that the swap() idiom can release the
memory.
If I push_back elements into the vector continuously and only call
erase() after use it, and I don't use that complex swap(), will this
vector eat up memory? Thank you.
Yes.
and uses up all memory, and the system crash eventually?
Specifically, if the container is a vector, you are guaranteed NOT toNoah said:Note that you're not guaranteed to get any memory back...especially if
the container is a vector.
It means the size that the vector is.[email protected] said:For example, ten elements are pushed into a newly created vector. Does
the size mean the numbers of elements in the vector or the memory that
the vector possesses? Does the size of the vector (memory?) increase
automatically to hold all elements?
[email protected] said:and uses up all memory, and the system crash eventually?
[email protected] said:[snip]
One people mentioned that the swap() idiom can release the
memory.
I'm not sure if that's guaranteed by the standard, but it's commonly used.
I'm up to no good, as usual.
I need to make sure a container is of a certain size. In 99.9% of cases
it already is. So, it makes sense to insert a test if(x.size() !=s) ,
unless it's redundant.
To rephrase the quesion in a language-lawyer-friendly format:
std::vector<int> v;
v.push_back(1);
int* p = &(v[0]);
v.resize(1);
assert(*p == 1); // guaranteed by the standard or not?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.