A vector is a dynamically sized sequential container that stores elements in contiguous order while managing an internal dynamic array. It supports appending, indexed access, insertion, and removal without manual storage-management code. section 6.2.2
Typical operations include:
- Append an element with push_back(). section 6.2.2
- Access an element by index with at(). section 6.2.2
- Insert or remove an element at a position using insert() and erase(). section 6.2.2
- Store either objects directly or pointers to objects, changing how copying and construction appear during operations. section 6.2.2
Appointments and Date objects
An Appointments class stores Date objects in a private vector. A test appends two dates, inserts a date at index 0, removes one at index 1, and prints the resulting sequence. Instrumenting Date reveals hidden copy construction during vector operations; replacing Date values with Date pointers removes those copy events in the demonstrated run, but requires pointer-based construction and access. section 6.2.2 section 6.2
Listing 6.11 (Program 6.7 SurpriseVector-1): Appointments.h (poor performance)
#include <vector>
#include "Date.h"
using namespace std;
class Appointments
{
public:
vector<Date> get_dates() const { return dates; }
Date at(const int index) const { return dates[index]; }
void append(const Date date) { dates.push_back(date); }
void insert(const int index, const Date date)
{
dates.insert(dates.begin() + index, date);
}
void remove(const int index)
{
dates.erase(dates.begin() + index);
}
private:
vector<Date> dates; #1
};
#1 Private vector of Date objects
Inspect the Appointments implementation that exposes indexed access, appending, insertion, removal, and a private vector of Date objects. section 6.2.2










