Topic: vector<T,Allocator> insert functions


Author: David Byrden <100101.2547@compuserve.com>
Date: 1996/02/21
Raw View
Karen;

>> As a minimum, I guess that begin() <= position <= end().  Are
>> insertion points allowed outside of these values?

  No. To insert outside of these limits would create a vector with some
'empty' places, i.e. uninitialised RAM. Without looking it up, I am pretty
sure that the intention of the template vector is that it hold a
contigous block of valid objects. Remember, when elements are shuffled
around, operator= is used on them without any check to see that they are
valid.

                         David
---
[ To submit articles: try just posting with your news-reader.
                      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.
]





Author: BXHG48B@prodigy.com (Karen Erner)
Date: 1996/02/20
Raw View
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
]