473,387 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Which functions change a vectors capacity?

Noo
Hi. I've got some code that uses vectors fairly extensively and it needs to
be efficient. Therefore I'm using reserve() quite a bit. However what other
functions (are supposed to) change the capacity?

I know that adding extra elements (e.g. by using push_back()) may do this,
if extra capacity is needed.

On the C++ implementation I'm using:
clear() seems to reset the capacity to zero.
resize() leaves the capacity as is.
operator=() does not copy the capacity. I thought for a while that, if
the elements fit into the vector on the left hand side of the = sign, the
capacity of this vector would remain the same, but copying an empty vector
seems to reset it to zero. So that after

vector<int> first;
first.reserve(1000);
Jul 19 '05 #1
4 2605
"Noo" <ar**@HHsys.uea.ac.uk> wrote...
Hi. I've got some code that uses vectors fairly extensively and it needs to be efficient. Therefore I'm using reserve() quite a bit. However what other functions (are supposed to) change the capacity?
All that may lead to 'insert', if such change is necessary.
I know that adding extra elements (e.g. by using push_back()) may do this,
if extra capacity is needed.
Why do you think you actually need the capacity? The whole idea
of 'reserving' some space is to [try to] avoid reallocations if
'push_back' is used.
On the C++ implementation I'm using:
clear() seems to reset the capacity to zero.
Even if it seems to, it doesn't necessarily do so.
resize() leaves the capacity as is.
Depends on whether you resize it beyond current capacity or not.
operator=() does not copy the capacity.
There is no requirement that it should.
I thought for a while that, if
the elements fit into the vector on the left hand side of the = sign, the
capacity of this vector would remain the same, but copying an empty vector
seems to reset it to zero. So that after

vector<int> first;
first.reserve(1000);
.
(Maybe inserting up to 1000 elements, maybe not)
.
vector<int> second;
second.reserve(1000);
second = first;

the capacity of second need not be 1000.
Correct. There is no such requirement in the language.

I was just wondering what other capacity changing functions I need to beware of!


'erase', 'insert', 'resize' (which is just an erase or an insert
depending on the current and required size), 'push_back' and
'push_front' (which are just short-cuts for 'insert'), all can
change the capacity of a vector.

Perhaps you need to explain what _problem_ you're trying to solve.

And, perhaps, getting and reading a good book on Standard Library
would help. I recommend the one by Nicolai Josuttis.

Victor
Jul 19 '05 #2

"Noo" <ar**@HHsys.uea.ac.uk> wrote in message news:bo**********@cpca14.uea.ac.uk...
I know that adding extra elements (e.g. by using push_back()) may do this,
if extra capacity is needed.


It is very uncommon for anything to shirnk capacity. reserve is NOT supposed
to. Most of the other changes aren't supposed to cause a reallocation (which you
would effectively have to do to shirnk capacity). I suspect from the description of
the behavior you are using VC++ 7. Microsoft says this behavior will go away in
the next release.

"swap()" will generally take the capacity with the swapped elements.
Jul 19 '05 #3
Noo

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Noo" <ar**@HHsys.uea.ac.uk> wrote in message news:bo**********@cpca14.uea.ac.uk...
I know that adding extra elements (e.g. by using push_back()) may do this, if extra capacity is needed.
It is very uncommon for anything to shirnk capacity. reserve is NOT

supposed to. Most of the other changes aren't supposed to cause a reallocation (which you would effectively have to do to shirnk capacity). I suspect from the description of the behavior you are using VC++ 7. Microsoft says this behavior will go away in the next release.


Great deduction there! And thanks for the help.

Alan
Jul 19 '05 #4
Noo

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:O0usb.181597$Tr4.505377@attbi_s03...
"Noo" <ar**@HHsys.uea.ac.uk> wrote...
Hi. I've got some code that uses vectors fairly extensively and it needs to be efficient. Therefore I'm using reserve() quite a bit. However what other functions (are supposed to) change the capacity?
All that may lead to 'insert', if such change is necessary.
I know that adding extra elements (e.g. by using push_back()) may do this, if extra capacity is needed.


Why do you think you actually need the capacity? The whole idea
of 'reserving' some space is to [try to] avoid reallocations if
'push_back' is used.


Understood. The problem I was having was that, after reserving space in the
vector, some other call was reducing the capacity. I only noticed when the
profiler showed me how much time was being taken by push_back() calls
On the C++ implementation I'm using:
clear() seems to reset the capacity to zero.


Even if it seems to, it doesn't necessarily do so.


Well, 'v.clear()' followed by 'cout << v.capacity()' gave the answer 0,
though whether it would always do that...
resize() leaves the capacity as is.


Depends on whether you resize it beyond current capacity or not.


Sorry. Meant to write resize(0).
And, perhaps, getting and reading a good book on Standard Library
would help. I recommend the one by Nicolai Josuttis.


Indeed, it's a great book! I'd be posting here more often without it!

Thanks for the help,

Alan
Jul 19 '05 #5

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

Similar topics

6
by: sks_cpp | last post by:
I am somewhat new to STL and the plethora of library functions they have. I looked at sgi's stl info. and I was overwhelmed so I thought I would present my question here. I have std::map<WWN*,...
3
by: Lasse Skyum | last post by:
Is it true that when std::vector resizes for more capacity it copies all the elements to a bigger array and then destroys all the elements from the old one? If so, why doesn't it just use...
19
by: chris | last post by:
Hello, I've recently been trying to understand the various structures supplied by c++, and the one I find most confusing is deque. One quick question about this. It seems most implementations...
8
by: slurper | last post by:
if i have a vector and assign another vector to a vector variable like this: vector<int> c, k; c.push_back(1), c.push_back(2); k.push_back(3), k.push_back(4); c = k; does this work?? if...
9
by: vsgdp | last post by:
Hi, Is there a place on the web that specifies the guaranteed order of STL functions? In particular, I want to know if std::vector::resize(x) is guaranteed to be constant running time. ...
5
by: Robert Fitzpatrick | last post by:
Can someone point me to some more information or perhaps show an example of returning a recordset from a plpgsql function. I'd like to send an argument or arguments to the function, do some queries...
9
by: Jeff | last post by:
Hello- Ive never used a vector or vectors in C++ so I have a question for you all. I know I can dynamically create the size I need upfront, but is it possible to create them on the fly...
5
by: vasim98 | last post by:
I am surprised to see the output of the following C++ program using vectors. The program is so simple. An element is pushed in a vector "v" and the pointer to this element is obtained as "ptr_v"....
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.