| Name: _____________________ | Class: CET 375 |
| SSN/ID: _____________________ | Section & Group: ____________ |
A. Introduction
Lab Exercise 5 dealt with C-style arrays, which are very non-OOPish in that they are not self-contained; that is, they are not objects that carry around with them information about themselves or operations on themselves. This is surely the case for non-char arrays for which there are very few predefined array operations. For example, an array does not keep track of the number of values currently stored in it (its size).
This lab exercise considers in detail the vector container
from STL (the Standard Template Library), which is an OOP counterpart
to one-dimensional arrays. It can be thought of as a
type-independent pattern for an array class whose
capacity can expand and which is self-contained.
More precisely, vector is a class template that can be used
to store and process elements of any type. A vector can expand its
capacity when necessary. And a vector has a large collection
of built-in operations that are implemented as function members. Its
declaration has the form:
where T is a parameter for the type of values to be stored in
the container.
template <typename T>
class vector {
// Details of vector omitted...
};
Example:
| vector<double> vec1; | vector<string> names; | ||
| ↓ | ↓ | ||
|
|
||
| ↓ | ↓ | ||
| Creates definition of class vector with each occurence of T replaced by double; uses this class to construct object named vec1 | Creates definition of class vector with each occurence of T replaced by string; uses this class to construct object named names |
Note: In this lab exercise, you will
use the program source_lab07_vectorlab.cpp to
experiment with vectors. Get a copy of this program by
right-clicking the link and copying/saving it to your home directory
and then rename it to vectorlab.cpp.
B. Defining a vector Object
vector has 4 constructors (and a copy constructor) which allow 4 different kinds of definitions of vector objects:
Examples (from vectorlab.cpp):
vector<int> v1;
vector<int> v2(2);
int numInts;
cin >> numInts;
vector<int> v3(numInts);
vector<int> v4(3, 99);
int a[] = (1, 4, 9, 16, 25);
vector<int> v5(a, a+5);
C. Member Functions dealing with capacity and size
| v.empty() | Return true if and only if v contains no values |
| v.size() | Return the number of values v contains |
| v.capacity() | Return the capacity of v; that is, the number of values it currently can store |
| v.max_size() | Return the maximum number of elements v can have (i.e., max. capacity) |
| v.reserve(n) | Increase capacity of v to n (does not affect size()) |
| vector | capacity | size_of | empty (Y or N) |
|---|---|---|---|
| v1 | |||
| v2 | |||
| v3 | |||
| v4 | |||
| v5 |
| vector | capacity | size_of |
|---|---|---|
| v4 |
v1: ___________________________________________________________
v2: ___________________________________________________________
v3: ___________________________________________________________
v4: ___________________________________________________________
v5: ___________________________________________________________
As you can see from the preceding:
| v2 | 0 | 0 |
| [0] | [1] |
| v4 | 99 | 99 | 99 |
| [0] | [1] | [2] |
and the reserve() function increases its capacity to 7 but doesn't change its size:
| v4 | 99 | 99 | 99 | ? | ? | ? | ? |
| [0] | [1] | [2] | [3] | [4] | [5] | [6] |
| a | a+1 | a+2 | a+3 | a+4 | a+5 | |
| ↓ | ↓ | ↓ | ↓ | ↓ | ↓ | |
| a | 1 | 4 | 9 | 16 | 25 | |
| [0] | [1] | [2] | [3] | [4] | ||
| ↓ | ↓ | ↓ | ↓ | ↓ | ||
| ↓ | ↓ | ↓ | ↓ | ↓ | ||
| v5 | 1 | 4 | 9 | 16 | 25 | |
| [0] | [1] | [2] | [3] | [4] |
D. Member Functions to append and remove values
| v.push_back(value); | Append value at v's end and increase v's size by 1 |
| v.pop_back(); | Erase v's last element and decrease v's size by 1 |
| vector | size | contents |
|---|---|---|
| v2 |
Add statements to append 22 to v2, and then output the size and contents of v2. Record the results here:
| vector | size | contents |
|---|---|---|
| v2 |
Add statements to append 33 to v2, and then output the size and contents of v2. Record the results here:
| vector | size | contents |
|---|---|---|
| v2 |
Now, add statements to erase the last element of v2, and then output the size and contents of v2. Record the results here:
| vector | size | contents |
|---|---|---|
| v2 |
|
|
E. A Fundamental Difference between vectors and Arrays
We have seen several differences between vectors and arrays. In particular, we have examined several of the built-in operations that vectors have but that arrays do not.
The preceding examples also indicate another basic difference between vectors and arrays. Even though the original capacity of v2 was 2, we were able to append 3 new values to v2 and its size increased each time. This would also suggest that its capacity increased.
And this is indeed the case. Although the capacity of an array is fixed, the capacity of a vector is increased automatically when necessary to accomodate a new value being appended. This is accomplished by allocating a larger block of memory (array) and copying the current values into it. The actual amount by which the capacity is increased is machine-dependent, but there are some patterns that are common to all implementations. We will investigate some of these now.
| v1 after: | capacity | size | contents |
|---|---|---|---|
| 1 value is added |
| v1 after: | capacity | size | contents |
|---|---|---|---|
| 1 value is added | |||
| 2 values are added | |||
| 3 values are added | |||
| 4 values are added | |||
| 5 values are added |
What seems to be the pattern in how the capacity is increasing?
| v1's old capacity | v1's new capacity |
|---|---|
What was the capacity of the vector before it increased for the first time?
____________________
What did the capacity become the first time it increased?
____________________
The second time it increased? ____________________
The third time it increased? ____________________
The fourth time it increased? ____________________
The fifth time it increased? ____________________
|
|
F. Member Functions to access the first and last values
| v.front(); | Returns a reference to the first value stored in v |
| v.back(); | Returns a reference to the back value stored in v |
_____________________________________________________________
Add statements to change the first element of v5 to 77
and the last element to 88 using these member functions. Write
your statements here:
_____________________________________________________________
_____________________________________________________________
Now, add a statement to output the contents of v5; give
the output produced here:
v5: _________________________________________________________
G. The Subscript Operator
Look at the definition of the function operator<<() at the beginning of vectorlab.cpp. Note the use of the subscript operator [] within a loop to output the contents of a vector. It is used in almost exactly the same way as for arrays. However, there is one important difference between vectors and C-style arrays.
|
|
The attempt above to access v6[0] may have caused a fatal
run-time error. If it did, comment out this statement before
proceeding.
H. Assignment, Relational, and Swap Operators
| v1 = v2 | Assigns to v1 a copy of v2 |
| v1 == v2 | Returns true if and only if v1 has the same values as v2, in the same order |
| v1 < v2 | Returns true if and only if v1 is lexicographically less than v2 |
| v1.swap(v2) | Swaps v1's contents with v2's |
Hand In:
| Category | Points Possible | Points Received |
|---|---|---|
| Lab Handout: | (50) | __________ |
| Program vectorlab.cpp | (50) | __________ |
TOTAL |
130 |
__________ |