473,396 Members | 2,029 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,396 software developers and data experts.

Non-Growing Container

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.
Mar 12 '08 #1
4 1240
D. Susman wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 12 '08 #2
On Mar 12, 10:30 am, "D. Susman" <derya.sus...@gmail.comwrote:
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.

Mar 12 '08 #3
D. Susman wrote:
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
Mar 12 '08 #4
On Mar 12, 3:30 pm, "D. Susman" <derya.sus...@gmail.comwrote:
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?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 13 '08 #5

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

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
0
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...
0
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,...

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.