For the Vector class available on Blackboard add an iterator
For the Vector class available on Blackboard, add an iterator insert(iterator pos, const Object & x) method. Note to move the iterators (which are really just pointers here) around, you need to add to them sizeOf(Object). For example, pos += sizeOf(Object); lets you examine the next item in the Object array.
Solution
QValueVector is a Qt implementation of an STL-like vector container. It can be used in your application if the standard vector is not available for your target platforms. QValueVector is part of the Qt Template Library.
QValueVector<T> defines a template instance to create a vector of values that all have the class T. QValueVector does not store pointers to the members of the vector; it holds a copy of every member. QValueVector is said to be value based; in contrast, QPtrList and QDict are pointer based.
QValueVector contains and manages a collection of objects of type T and provides random access iterators that allow the contained objects to be addressed. QValueVector owns the contained elements. For more relaxed ownership semantics, see QPtrCollection and friends, which are pointer-based containers.
QValueVector provides good performance if you append or remove elements from the end of the vector. If you insert or remove elements from anywhere but the end, performance is very bad. The reason for this is that elements must to be copied into new positions.
Some classes cannot be used within a QValueVector: for example, all classes derived from QObject and thus all classes that implement widgets. Only values can be used in a QValueVector. To qualify as a value the class must provide:
Note that C++ defaults to field-by-field assignment operators and copy constructors if no explicit version is supplied. In many cases this is sufficient.
QValueVector uses an STL-like syntax to manipulate and address the objects it contains. See this document for more information.
Example:
Program output:
As you can see, the most recent change to Joe\'s salary did not affect the value in the vector because the vector created a copy of Joe\'s entry.
Many Qt functions return const value vectors; to iterate over these you should make a copy and iterate over the copy.
There are several ways to find items in the vector. The begin() and end() functions return iterators to the beginning and end of the vector. The advantage of getting an iterator is that you can move forward or backward from this position by incrementing/decrementing the iterator. The iterator returned by end() points to the element which is one past the last element in the container. The past-the-end iterator is still associated with the vector it belongs to, however it is not dereferenceable; operator*() will not return a well-defined value. If the vector is empty(), the iterator returned by begin() will equal the iterator returned by end().
The fastest way to access an element of a vector is by using operator[]. This function provides random access and will return a reference to the element located at the specified index. Thus, you can access every element directly, in constant time, providing you know the location of the element. It is undefined to access an element that does not exist (your application will probably crash). For example:
Whenever inserting, removing or referencing elements in a vector, always make sure you are referring to valid positions. For example:
The iterators provided by vector are random access iterators, therefore you can use them with many generic algorithms, for example, algorithms provided by the STL or the QTL.
Another way to find an element in the vector is by using the std::find() or qFind() algorithms. For example:
It is safe to have multiple iterators on the vector at the same time. Since QValueVector manages memory dynamically, all iterators can become invalid if a memory reallocation occurs. For example, if some member of the vector is removed, iterators that point to the removed element and to all following elements become invalidated. Inserting into the middle of the vector will invalidate all iterators. For convenience, the function back() returns a reference to the last element in the vector, and front() returns a reference to the first element. If the vector is empty(), both back() and front() have undefined behavior (your application will crash or do unpredictable things). Use back() and front() with caution,


