Non-Growing Container

D

D. Susman

Hi,

Say I have a vector v. Is there a way to limit its size (capacity?)
initially so that it does not grow due to insertions? Would setting
the capacity as a fixed number do the trick? If so, what happens when
capacity is reached?

Thanks.
 
V

Victor Bazarov

D. Susman said:
Say I have a vector v. Is there a way to limit its size (capacity?)
initially so that it does not grow due to insertions? Would setting
the capacity as a fixed number do the trick? If so, what happens when
capacity is reached?

No, the standard containers do not have the ability to be limited.
Use a regular array for that, or roll your own container wrapping
std::vector, for example. Keep in mind that you shouldn't derive
from it publicly because when you put a limitation on a type, it
breaks the LSP. Derive privately and reimplement all functionality
you need along with introducing the new features. You can easily
implement the initial size and prohibit growth (by simply not
implementing 'push_back' or 'insert').

V
 
E

Eric.Malenfant

Hi,

Say I have a vector v. Is there a way to limit its size (capacity?)
initially so that it does not grow due to insertions? Would setting
the capacity as a fixed number do the trick? If so, what happens when
capacity is reached?

As far as I know, not with std::vector. TR1 includes an "array"
container. You may also be interested in Boost.Array (http://
www.boost.org/doc/html/array.html) which, IIUC, served as a base for
the TR1 array proposal.
 
B

Bo Persson

D. Susman said:
Hi,

Say I have a vector v. Is there a way to limit its size (capacity?)
initially so that it does not grow due to insertions? Would setting
the capacity as a fixed number do the trick? If so, what happens
when capacity is reached?

You can use the reserve() fucntion to set a capacity.

It is then up to you to decide what happens when you run out of
capacity

if (v.size() < v.capacity())
{
v.push_back(something);
}
else
{
// You decide what happens!
}


Bo Persson
 
J

James Kanze

Say I have a vector v. Is there a way to limit its size
(capacity?) initially so that it does not grow due to
insertions?

Not really. You can probably come up with some way using a user
defined allocator, however. But...
Would setting the capacity as a fixed number do the trick? If
so, what happens when capacity is reached?

That's the real question. If you support insertion, do you want
to limit the capacity, and if so, what do you do when you reach
the limit? (It's easy to limit the capacity artificially, of
course, by checking the size() before inserting. In which case,
you can do whatever you want.)

What problem are you trying to solve?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top