473,657 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector's reserve(), erase() and clear()

What is relation between std::vector's reserve() and erase()/clear()?

vector<int> v;

v.reserve(100);
v.resize(100);
v.erase(v.end() );

How many elements are reserved here: 100 or 99?

v.reserve(200);
v.resize();
v.clear();

How many elements are reserved here: 200 or 0?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Sep 23 '05 #1
8 10645

Alex Vinokur 写道:
What is relation between std::vector's reserve() and erase()/clear()?

vector<int> v;

v.reserve(100);
v.resize(100);
v.erase(v.end() );

How many elements are reserved here: 100 or 99?

v.reserve(200);
v.resize();
v.clear();

How many elements are reserved here: 200 or 0?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


you can test it on you computer,it's easy!

Sep 23 '05 #2

"Alex Vinokur" <al****@go.to > wrote in message
news:dg******** **@reader.great nowhere.com...
What is relation between std::vector's reserve() and erase()/clear()?

vector<int> v;

v.reserve(100);
v.resize(100);
v.erase(v.end() );
This last line will produce undefined behavior. 'end()'
points to one element past the last element in
the vector. You can't 'erase' it.

How many elements are reserved here: 100 or 99?
Exactly the number you used: 100
Then undefined behavior happens.

v.reserve(200);
v.resize();
This is an error. At least one argument (the new size)
is required.
v.clear();

How many elements are reserved here: 200 or 0?


Exactly the number you used: 200
But the call to resize is in error.

-Mike
Sep 23 '05 #3

<us******@gmail .com> wrote in message news:11******** **************@ g14g2000cwa.goo glegroups.com.. .

Alex Vinokur ??:
What is relation between std::vector's reserve() and erase()/clear()?

[snip]
you can test it on you computer,it's easy!
====== foo.cpp ======
#include <vector>
#include <iostream>
using namespace std;

#define SHOW cout << "capacity = " << v.capacity() << "\t size = " << v.size() << endl;

int main()
{
vector<int> v;
SHOW;
v.reserve(100);
SHOW;
v.resize(100);
SHOW;
v.erase(v.end() - 1);
SHOW;
v.clear();
SHOW;

return 0;
}
=============== ======

1. Output for GNU g++ 3.4.4, GNU gpp 4.0.1, Bolrand C++ 5.5.1, Digital Mars 8.38n
---------------------------
capacity = 0 size = 0
capacity = 100 size = 0
capacity = 100 size = 100
capacity = 100 size = 99
capacity = 100 size = 0
---------------------------
2. Output for Microsoft C++ 13.00.9466
---------------------------
capacity = 0 size = 0
capacity = 100 size = 0
capacity = 100 size = 100
capacity = 100 size = 99
capacity = 0 size = 0
---------------------------
We can see that capacity == 0 after clear() for Microsoft C++.
Is it correct behavior?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Sep 23 '05 #4

"Mike Wahler" <mk******@mkwah ler.net> wrote in message news:AP******** *********@newsr ead3.news.pas.e arthlink.net...

"Alex Vinokur" <al****@go.to > wrote in message [snip]
v.erase(v.end() );


This last line will produce undefined behavior. 'end()'
points to one element past the last element in
the vector. You can't 'erase' it.


Thanks. Updated in my new message at
http://groups.google.com/group/comp....ad33960f339af5

How many elements are reserved here: 100 or 99?
Exactly the number you used: 100
Then undefined behavior happens.

v.reserve(200);
v.resize();


This is an error.

Of course, my mistake. At least one argument (the new size)
is required.
v.clear();

How many elements are reserved here: 200 or 0?
Exactly the number you used: 200

See my new message at
http://groups.google.com/group/comp....ad33960f339af5 But the call to resize is in error.

-Mike


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Sep 23 '05 #5
> How many elements are reserved here: 100 or 99?

v.capacity() could tell you if you fix the erase().
How many elements are reserved here: 200 or 0?


v.capacity() could tell you if you fix the resize().

Sep 23 '05 #6

us******@gmail. com schreef:
Alex Vinokur 写道:
What is relation between std::vector's reserve() and erase()/clear()?


you can test it on you computer,it's easy!


Not really. I.e. If I call reserve(100), and get capacity()==128 ,
that's OK. However, you might get capacity()==100 and that's OK too.
You can't induce the C++ standard.

HTH,
Michiel Salters

Sep 23 '05 #7
"Alex Vinokur" <al****@go.to > wrote in message
news:dg******** **@reader.great nowhere.com...
What is relation between std::vector's reserve() and erase()/clear()?
v.reserve(n) sets the capacity to at least max(n, v.size()) and leaves the
size unchanged.
v.erase reduces the size by the number of elements erased and does not
change the capacity.
v.clear sets the size to 0 and does not change the capacity.
vector<int> v;
v's size is now 0.
v.reserve(100);
v's size is now 0 and its capacity is >= 100.
v.resize(100);
v's size is now 100 and its capacity is >= 100.
v.erase(v.end() );
This is undefined behavior because v.end() does not refer to an element.
How many elements are reserved here: 100 or 99? v.reserve(200);
v's size is now 100 and its capacity is >= 200.
v.resize();
This should not compile, as you did not specify a size.
v.clear();


v's size is now 0 and its capacity is >= 200.
Sep 23 '05 #8

"Andrew Koenig" <ar*@acm.org> wrote in message news:Vv******** *************@b gtnsc05-news.ops.worldn et.att.net...
[snip]
v.clear sets the size to 0 and does not change the capacity.

[snip]

The issue was discussed in microsoft.publi c.vc.language as well.
See Tom Widmer [VC++ MVP]'s message at
http://groups.google.com/group/micro...297d544d6a8a18
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Sep 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
7066
by: Stefan Hhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to clear(), in DOT.NET the capacity() is reduced to 0.
4
12235
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
11
8870
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse perfomance than if I first define the vector of a given size, then write to the elements with myvec = However, I'm currently thinking that it isn't feasible to obtain the vector size, so really need to resize the vector dynamically as I go. Is...
6
8015
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if only achieves half the job. Here is how I would use std::remove_if to remove elements from std::vector based on predicate: v.erase(std::remove_if(v.begin(), v.end(), pred), v.end()); After that line is executed I cannot get back the elements...
8
5104
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
8
4893
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike std::vector::erase, it calls T::operator= only three times no matter
7
3005
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of 501st element is going to cause some more allocation -- for arguments sake if the vector re-grows to accomodate 1000 elements, I play around with it and completely erase everything in it after I am done. Now how much does the vector hold? Do I...
5
32485
by: atreya | last post by:
Hi, I'm trying to figure out if there are any differences between the implementation of resize(0) and clear() that a programmer using std::vector should be aware of! Could there be any advantage/disadvantage of using one over the other? I checked the VS7.1 implementation, in which I couldn't find any difference. Both of them are effectively calling erase(begin(), end()).
23
3466
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12) The program below fails, but if the reserve(en)
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8323
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.