liveBook · Manning

Vector

As explained in Object-Oriented Software Design in C++

A dynamically sized sequential container that stores elements in contiguous order and manages an internal dynamically allocated array.

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

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains storage resizing, core operations, object copying, and a pointer-based alternative, but does not quantify complexity.
Examples
Demonstrates vectors through book catalogues, appointment dates, insertion, removal, printed output, and instrumented copy-constructor traces.
What it assumes
Assumes basic C++ classes, objects, pointers, indexing, and insertion or removal operations.
What you can do after
Evaluate vector usage and diagnose performance surprises caused by storing objects and triggering hidden copies.
How it connects
Connects vectors with arrays, pointers, objects, copy constructors, assignment operators, and dynamic storage.

Also explained in

Compare how 11 books explain vector →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.