Article View: comp.std.c++
Article #3387vector<T,Allocator> insert functions
From: Karen Erner
Date: Tue, 20 Feb 1996 18:47
Date: Tue, 20 Feb 1996 18:47
78 lines
3659 bytes
3659 bytes
In attempting to implement the vector<T, Allocator> class, I have two
questions about the insert() functions. I am working from the April 1995
working draft, so I apologize if any of my questions have already been
resolved.
Under subclause 23.2.5.6 vector modifiers, we have rules for the insert()
functions. I read the following:
"Notes: Causes reallocation if the new size is greater than the old
capacity. If no reallocation happens, all the iterators and references
before the insertion point remain valid."
What is not clear to me, and this is probably perfectly obvious, but what
insertion points are allowed? This is my first question. As a minimum,
I guess that begin() <= position <= end(). Are insertion points allowed
outside of these values? I ask this because we are allowed to reserve
and reallocate memory. For example, here is a case where the new size
does not exceed the capacity, and the capacity exceeds the old size. Let
us also say that we have some print function that writes values from
begin() to end() to standard output, and that ## denotes denotes the
result of printing uninitialized memory.
vector<int allocator> V(3);
cout << V.size() << endl; // prints 3
cout << V.capacity() << endl; // prints 3
V.print(); // prints 0 0 0
V.reserve(5);
cout << V.size() << endl; // prints 3
cout << V.capacity() << endl; // prints 5
V.print(); // prints 0 0 0
Can we insert a value at the new fifth position?
V.insert(V.end() + 2, 12); // insert 12 at 5th position
V.print(); // prints 0 0 0 ## 12
Or a value before the first position?
V.insert(V.begin() - 1, 12); // insert 12 before 1st position
V.print(); // 12 ## 0 0 0
If these cases are permissable, would it also be allowable to use these
insertion points without previously reserving the required space forcing
a reallocation?
As my second question, I see that we have:
iterator insert(iterator position, const T& x = T());
void insert(iterator position, size_type n, const T& x);
template <class T>
void insert(iterator position, InputerIterator first, InputIterator last);
Shouldn't we also be able to insert vectors, like
void insert(iterator position, const vector<T,
Allocator>& v);
.. or better still ...
template <Allocator2>
void insert(iterator position,
const vector<T, Allocator2>& v); ?
I apologize once again, if I am asking questions gone over before.
Karen
---
[ To submit articles: Try just posting with your newsreader. If that fails,
use mailto:std-c++@ncar.ucar.edu
FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
Comments? mailto:std.c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<4g892r$1ave@usenetp1.news.prodigy.com>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: BXHG48B@prodigy.com (Karen Erner) Newsgroups: comp.std.c++ Subject: vector<T,Allocator> insert functions Date: 20 Feb 1996 10:47:09 PST Organization: Prodigy Services Company 1-800-PRODIGY Approved: austern@isolde.mti.sgi.com Message-ID: <4g892r$1ave@usenetp1.news.prodigy.com> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 18 Feb 1996 22:26:35 GMT X-Newsreader: Version 1.2 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMSoXO0y4NqrwXLNJAQG9LwH/cPoLkVDllEw2C2GCYxcOHDtv/IPZpY/x A05EYfXTbNXaHRhgIIEO/zLxhCy8hBdctI4da7JuHoJ2K0X6frj8Iw== =LKAm Originator: austern@isolde.mti.sgi.com