473,804 Members | 3,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector::reserve () with value less than current size().

What is the official word on what happens if I call reserve() on an
std::vector, and specify a capacity that is less than the current
*size* (not capacity) of that vector? Is it supposed to resize() the
vector as well? Or just not modify anything?

Thanks,
Jason
Nov 19 '08 #1
3 2542
ja************@ gmail.com wrote:
What is the official word on what happens if I call reserve() on an
std::vector, and specify a capacity that is less than the current
*size* (not capacity) of that vector? Is it supposed to resize() the
vector as well? Or just not modify anything?

Thanks,
Jason
reserve() never reduces capacity; it just increases it. Therefore this
is a no-op.

Joe Gottman
Nov 19 '08 #2
On Nov 18, 8:25 pm, "jason.cipri... @gmail.com"
<jason.cipri... @gmail.comwrote :
What is the official word on what happens if I call reserve() on an
std::vector, and specify a capacity that is less than the current
*size* (not capacity) of that vector? Is it supposed to resize() the
vector as well? Or just not modify anything?

Thanks,
Jason
The size of the vector is irrelevant. If reserve's arguement is less
than or equal to its capacity, nothing happens. Swap the vector to
(maybe) change its capacity.

Nov 19 '08 #3
On Nov 18, 8:48*pm, Joe Gottman <jgott...@carol ina.rr.comwrote :
jason.cipri...@ gmail.com wrote:
What is the official word on what happens if I call reserve() on an
std::vector, and specify a capacity that is less than the current
*size* (not capacity) of that vector? Is it supposed to resize() the
vector as well? Or just not modify anything?
Thanks,
Jason

reserve() never reduces capacity; it just increases it. *Therefore this
is a no-op.
That's what I was looking for, thanks Joe and Salt_Peter.

Also, sorry, I'm looking now and it turns out that actually *is*
stated in the docs I was reading, it's just in a footnote that I
missed. :-o

Jason
Nov 19 '08 #4

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

Similar topics

5
5665
by: john smith | last post by:
HI, when I try the following code, I get a segfault when compiled with VC.NET and g++ under cygwin. #1 vector<int> vi; #2 vector<int>::iterator ii = vi.begin(); #3 vi.reserve(10); #4 for(int i = 0; i < 10; ++i) { #5 *ii++ = 4; #6 } #7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
18
494
by: Alex Vinokur | last post by:
Hi, vector<T> v (100); T *a = new T ; v.push_back(T()); Is it possible to add new T to array to get _contiguous_ storage area (for 101 element in an example above)? -- Alex Vinokur
2
1920
by: Alex Vinokur | last post by:
Method vector.reserve() returns no value. How can we prevent the following situation? ====== foo.cpp ====== #include <climits> #include <vector> #include <iostream> using namespace std; int main()
2
5247
by: Gary Kuehn | last post by:
Is Reserve guaranteed to allocate contiguous memory? How safe is the following: vector<char> vbuff; int sz = numeric_limits<short int>::max();
7
3017
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
2084
by: quat | last post by:
Hi, Is it not legal to access an element that has been reserved? For example: vector<int> x; x.reserve(10); x = 1;
3
5014
by: pkirk25 | last post by:
vector<stringbuf_string; buf_string.reserve(256); vector<intbuf_mat_prices; buf_mat_prices.reserve(1000); During loops I fill the vectors and then I empty them with commands like buf_string.clear(); buf_mat_prices.clear(); Does this mean that the memory allocation returns to default or is my
9
2607
by: Chris Roth | last post by:
I have a vector of vectors: vector< vector<double v; and have initialized it with: v( 5 ); as I know I will have 5 columns of data. At this point, I read text file data into each of the the vectors using push_back. I know that I will be reading in 5000 elements into each vector, so I use reserve: ifstream f( "file.txt" ); if(f.is_open()) {
23
3486
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
9706
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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,...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7616
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
6851
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
5520
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...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
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

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.