473,624 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector: reserve required?

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)
is uncommented, it works. Is this as expected?

// vectst.cc 07/04/08

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, const char* argv[])
{
int en = 10;
vector<int vec;

// vec.reserve(en) ;
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;

exit (0);
}

Thanks,
Mike.
Jul 4 '08 #1
23 3462

Mike -- Email Ignored wrote:
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)
is uncommented, it works. Is this as expected?

// vectst.cc 07/04/08

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, const char* argv[])
{
int en = 10;
vector<int vec;

// vec.reserve(en) ;
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;

exit (0);
}

Thanks,
Mike.

No, you could do that (use reserve), or, use push_back()

int main(int argc, const char* argv[])
{
int en = 10;
vector<int vec;

for (int jj = 0; jj < en; ++jj)
vec.push_back(j j);
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;

return 0;
}
Jul 4 '08 #2
Mike -- Email Ignored wrote:
In std::vector, is reserve or resize required?
No, but you need to fill the vector somehow. You can do that either upon
construction, or by using push_back, insert, or resize.

The reserve() method, on the other hand does not grow the vector.
>
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)
is uncommented, it works.
It does not. You are observing a manifestation of undefined behavior.
Is this as expected?
There are no expectations as to how undefined behavior will show itself.

From a quality of implementation point of view, would want to see an abort
if you compile the program with assertions turned on.

// vectst.cc 07/04/08

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, const char* argv[])
{
int en = 10;
vector<int vec;

// vec.reserve(en) ;
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;
Try something like

cout << vec.size() << endl;

and ponder the meaning of what you get.
>
exit (0);
}

Best

Kai-Uwe Bux
Jul 4 '08 #3
On Jul 4, 7:44*am, Darío Griffo <dario.griffo.l is...@gmail.com wrote:
Mike -- Email Ignored wrote:
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)
is uncommented, it works. *Is this as expected?
// vectst.cc 07/04/08
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char* argv[])
{
* *int * * * * * * * * *en = 10;
* *vector<int* * * * *vec;
// * vec.reserve(en) ;
* *for (int jj = 0; jj < en; ++jj)
* * * vec[jj] = jj;
* *for (int jj = 0; jj < en; ++jj)
* * * cout << vec[jj] << endl;
* *exit (0);
}
Thanks,
Mike.

No, you could do that (use reserve), or, use push_back()
reserve() does not make the vector larger. You must have meant "you
could use resize or push_back."

Ali
Jul 4 '08 #5
On Jul 4, 7:54*am, callumurquh...@ googlemail.com wrote:
http://www.cplusplus.com/reference/stl/vector/operator[].html
If the OP did not concentrate on the vec.reserve(en) , the document you
show could be used to help with the problem. But because the OP does
expect some behavior from vec.reserve(en) , the problem is not with
operator[].

Ali
Jul 4 '08 #6
On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:
On Jul 4, 7:54Â*am, callumurquh...@ googlemail.com wrote:
>http://www.cplusplus.com/reference/stl/vector/operator[].html

If the OP did not concentrate on the vec.reserve(en) , the document you
show could be used to help with the problem. But because the OP does
expect some behavior from vec.reserve(en) , the problem is not with
operator[].

Ali
Another test using:
vec.reserve(en) ;
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
cout << "vec.size() = " << vec.size() << endl;

prevents the crash, but vec.size() returns zero, showing that
in this case, reserve() really does not work.

Mike.
Jul 4 '08 #7

acehr...@gmail. com wrote:
reserve() does not make the vector larger. You must have meant "you
could use resize or push_back."

Ali
Yes, you're right
Jul 4 '08 #8
LR
Mike -- Email Ignored wrote:
On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:
>On Jul 4, 7:54 am, callumurquh...@ googlemail.com wrote:
>>http://www.cplusplus.com/reference/stl/vector/operator[].html
If the OP did not concentrate on the vec.reserve(en) , the document you
show could be used to help with the problem. But because the OP does
expect some behavior from vec.reserve(en) , the problem is not with
operator[].

Ali

Another test using:
vec.reserve(en) ;
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
cout << "vec.size() = " << vec.size() << endl;

prevents the crash, but vec.size() returns zero,
What does vec.capacity() return?
showing that
in this case, reserve() really does not work.
I think it doesn't do what you expect. But why do you think it doesn't work?

Please consider this:
#include <iostream>
#include <vector>

int main() {
const unsigned int en = 10;
std::vector<int v;
//
std::cout << "a " << v.size() << " " << v.capacity() << std::endl;
v.reserve(en);
std::cout << "b " << v.size() << " " << v.capacity() << std::endl;
//
for(unsigned int i=0; i<en; i++) {
v[i] = i;
}
//
v.push_back(-8);
std::cout << "c " << v.size() << " " << v.capacity() << std::endl;
//
for(unsigned int i=0; i<en; i++) {
std::cout << "[" << i << "] " << v[i] << std::endl;
}
}
Also, please consider this:
#include <iostream>
#include <vector>

int main() {
const unsigned int en = 10;
std::vector<int v;
std::cout << "a " << v.size() << " " << v.capacity() << std::endl;
v.resize(en); // there's more than one way to do it
std::cout << "b " << v.size() << " " << v.capacity() << std::endl;
//
v[0] = -8;
v[9] = -7;
v.push_back(-9);
std::cout << "c " << v.size() << " " << v.capacity() << std::endl;
std::copy(v.beg in(), v.end(), std::ostream_it erator<int>(std ::cout,
" "));
}
LR
Jul 4 '08 #9
Mike -- Email Ignored wrote:
On Fri, 04 Jul 2008 08:30:37 -0700, acehreli wrote:
>On Jul 4, 7:54 am, callumurquh...@ googlemail.com wrote:
>>http://www.cplusplus.com/reference/stl/vector/operator[].html
If the OP did not concentrate on the vec.reserve(en) , the document you
show could be used to help with the problem. But because the OP does
expect some behavior from vec.reserve(en) , the problem is not with
operator[].

Ali

Another test using:
vec.reserve(en) ;
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
cout << "vec.size() = " << vec.size() << endl;

prevents the crash, but vec.size() returns zero, showing that
in this case, reserve() really does not work.
No, reserve() does work. You misunderstand how it does.
vector<>::reser ve changes the CAPACITY -- that is, you can use
push_back() or resize() up to the amount you've reserved without
the vector reallocating. It does not change the SIZE of the vector.

Try this:

#include <iostream>
#include <ostream>
#include <vector>

using namespace std;

int main()
{
vector<intv;

cout << "size = " << v.size() << "\n"
<< "capacity = " << v.capacity << "\n";

v.reserve(200);

cout << "size = " << v.size() << "\n"
<< "capacity = " << v.capacity << "\n";

v.resize(200);

cout << "size = " << v.size() << "\n"
<< "capacity = " << v.capacity << endl;

return 0;
}
Jul 4 '08 #10

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

Similar topics

10
7064
by: Stefan Höhne | 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:
18
2861
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
11
8869
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...
20
17809
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
8
10644
by: Alex Vinokur | last post by:
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?
8
5103
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...
7
3003
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...
4
2312
by: mathieu | last post by:
Hello, I am looking at the API of std::vector but I cannot find a way to specify explicitely the size of my std::vector. I would like to avoid vector::resize since it first initializes the elements of the vector. Thank you ! Mathieu Code:
0
8251
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
8182
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
8352
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
8494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6115
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
5570
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
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
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
1496
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.