Dear all,
We know that a vector can increase its capacity.
Does it mean that system will allocate more memory to fit the value of
capacity?
If yes, then we maybe cost memory if capacity is greater than its size.
To use resize() function, it can't reduce capacity.
How to reduce capacity to zero or delete a vector type variable completely?
Thanks for your answer.
Regards,
cylin. 7 2439
"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-berlin.de... Dear all,
We know that a vector can increase its capacity. Does it mean that system will allocate more memory to fit the value of capacity?
Yes
If yes, then we maybe cost memory if capacity is greater than its size. To use resize() function, it can't reduce capacity. How to reduce capacity to zero or delete a vector type variable
completely? Thanks for your answer.
There's a trick.
vector<int> x;
....
x.swap(vector<int>());
The default constructed vector will have zero capacity, so swapping that
with vector x will give vector x zero capacity. Regards, cylin.
john
"John Harrison" <jo*************@hotmail.com> ¼¶¼g©ó¶l¥ó·s»D
:bg************@ID-196037.news.uni-berlin.de... "cylin" <cy***@avant.com.tw> wrote in message news:bg************@ID-154203.news.uni-berlin.de... Dear all,
We know that a vector can increase its capacity. Does it mean that system will allocate more memory to fit the value of capacity? Yes
If yes, then we maybe cost memory if capacity is greater than its size. To use resize() function, it can't reduce capacity. How to reduce capacity to zero or delete a vector type variable completely? Thanks for your answer.
There's a trick.
vector<int> x; ... x.swap(vector<int>());
If I do this, the total momery will release the part of x?
Or the total memory is still the same?
Thanks The default constructed vector will have zero capacity, so swapping that with vector x will give vector x zero capacity.
Regards, cylin.
john
"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-berlin.de... "John Harrison" <jo*************@hotmail.com> ¼¶¼g©ó¶l¥ó·s»D :bg************@ID-196037.news.uni-berlin.de... "cylin" <cy***@avant.com.tw> wrote in message news:bg************@ID-154203.news.uni-berlin.de... Dear all,
We know that a vector can increase its capacity. Does it mean that system will allocate more memory to fit the value of capacity?
Yes
If yes, then we maybe cost memory if capacity is greater than its
size. To use resize() function, it can't reduce capacity. How to reduce capacity to zero or delete a vector type variable completely? Thanks for your answer.
There's a trick.
vector<int> x; ... x.swap(vector<int>());
If I do this, the total momery will release the part of x? Or the total memory is still the same? Thanks
This will release the memory of x. After the swap the memory of x will be
contained in the temporary object that is created with vector<int>(). The
destructor for that temporary object will release the memory that was
formerly in x. The destructor for the temporary object is called before the
next statement executes.
john
John Harrison wrote: This will release the memory of x. After the swap the memory of x will be contained in the temporary object that is created with vector<int>(). The destructor for that temporary object will release the memory that was formerly in x. The destructor for the temporary object is called before the next statement executes.
john
For some reason my reply doesn't seem to have shown up yet, but I
mentioned in that reply that a vector may have a non-zero minimum
capacity, therefore the swap-with-a-temporary idiom doesn't necessarily
do exactly what the OP asked (make the capacity 0).
-Kevin
"John Harrison" <jo*************@hotmail.com> ¼¶¼g©ó¶l¥ó·s»D
:bg************@ID-196037.news.uni-berlin.de... "cylin" <cy***@avant.com.tw> wrote in message news:bg************@ID-154203.news.uni-berlin.de... "John Harrison" <jo*************@hotmail.com> ¼¶¼g©ó¶l¥ó·s»D :bg************@ID-196037.news.uni-berlin.de... "cylin" <cy***@avant.com.tw> wrote in message news:bg************@ID-154203.news.uni-berlin.de... > Dear all, > > We know that a vector can increase its capacity. > Does it mean that system will allocate more memory to fit the value
of > capacity?
Yes
> If yes, then we maybe cost memory if capacity is greater than its size. > To use resize() function, it can't reduce capacity. > How to reduce capacity to zero or delete a vector type variable completely? > Thanks for your answer.
There's a trick.
vector<int> x; ... x.swap(vector<int>()); If I do this, the total momery will release the part of x? Or the total memory is still the same? Thanks
This will release the memory of x. After the swap the memory of x will be contained in the temporary object that is created with vector<int>(). The destructor for that temporary object will release the memory that was formerly in x. The destructor for the temporary object is called before
the next statement executes.
john
I See. Thanks,john.
Regards,
cylin.
John> There's a trick.
John> vector<int> x;
John> ...
John> x.swap(vector<int>());
Not quite. The trouble is that vector<int>() is an rvalue, and
the argument to swap requires an lvalue.
So you have to write it this way:
vector<int>().swap(x);
which relies on the fact that it is permissible to call the swap
member of an rvalue.
--
Andrew Koenig, ar*@acm.org
"Andrew Koenig" <ar*@acm.org> wrote in message
news:yu**************@tinker.research.att.com... John> There's a trick.
John> vector<int> x; John> ... John> x.swap(vector<int>());
Not quite. The trouble is that vector<int>() is an rvalue, and the argument to swap requires an lvalue.
So you have to write it this way:
vector<int>().swap(x);
which relies on the fact that it is permissible to call the swap member of an rvalue.
-- Andrew Koenig, ar*@acm.org
OK, thanks. Obviously one of the things my compiler doesn't complain about.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tran Tuan Anh |
last post by:
Dear all,
I am new in C++, and now get confused about a lot of things.
I wrote this simple code to test the vector.
class Temp { public: int x; };
int main() {
vector<Temp> v;
|
by: John |
last post by:
Thanks a lot.
How about "Vector"? If I put a Vector in a structure, the size of the
structure may dynamically change, right?
John
Gregg <gregg@invalid.invalid> wrote in message...
|
by: John |
last post by:
Hi all:
I write the following code about vector and list. Why the result is
not correct?
Thanks.
John
--------------------------------------------------------------
|
by: kathy |
last post by:
I am using std::vector in my program:
func()
{
std::vector <CMyClass *> vpMyClass;
vpMyClass.push_back(new CMyClass());
vpMyClass.push_back(new CMyClass());
vpMyClass.push_back(new...
|
by: Jef Driesen |
last post by:
I'm working on a project where i need to exchange multidimensional data
between C/C++ (row-major) and matlab (column-major). I understand the
difference between those two mappings to linear memory....
|
by: madhu |
last post by:
vector<vector<vector<long Vector3D; // 3dvector.
for (long k = 0; j < Depth; j++ )
{
Vector3D.push_back ( vector<vector<A_Type() );
for (long j = 0; j < Height; j++ )
{
Vector3D.push_back (...
|
by: liuhaoran |
last post by:
HI. i have a question about memory error.
when i change double variable to float variable ,for example:
int curGen = 0;
double sum = 0;
// m_iPopSize is int variable ,NewPop is a vector...
|
by: remlostime |
last post by:
now, i write some code
code1:
int a;
int main(){}
code2:
vector<inta(10000000);
int main(){}
after using g++ compile, and run it, code1 is broken, but code2 runs
|
by: cham |
last post by:
Hi,
I am working on c++ in a linux system ( Fedora core 4 ),
kernel version - 2.6.11-1.1369_FC4
gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 )
In my code i am creating a vector to store...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |