StanfordCPPLib
Public Types | Public Member Functions | List of all members
Vector< ValueType > Class Template Reference

This class stores an ordered list of values similar to an array. More...

#include "vector.h"

Public Types

using const_iterator = stanfordcpplib::collections::CheckedIterator< typename ContainerType::const_iterator >
 
using iterator = stanfordcpplib::collections::CheckedIterator< typename ContainerType::iterator >
 

Public Member Functions

 Vector()=default
 Initializes a new empty vector. More...
 
 Vector(int n, ValueType value=ValueType())
 Initializes a new vector, creating an array with n elements, each of which is initialized to value. More...
 
 Vector(std::initializer_list< ValueType > list)
 Uses an initializer list to set up the vector. More...
 
~Vector()=default
 Frees any heap storage allocated by this vector. More...
 
void add(const ValueType &value)
 Adds a new value to the end of this vector. More...
 
Vector< ValueType > & addAll(const Vector< ValueType > &v)
 Adds all elements of the given other vector to this vector. More...
 
ValueType & back()
 Returns the element at index (size - 1) in this vector (without removing it). More...
 
const ValueType & back() const
 Returns the element at index (size - 1) in this vector (without removing it). More...
 
iterator begin()
 
const_iterator begin() const
 
void clear()
 Removes all elements from this vector. More...
 
bool contains(const ValueType &value) const
 Returns true if the vector contains the given value. More...
 
iterator end()
 
const_iterator end() const
 
void ensureCapacity(int cap)
 Guarantees that the vector's internal array is at least the given length. More...
 
bool equals(const Vector< ValueType > &v) const
 Compares two vectors for equality. More...
 
ValueType & front()
 Returns the element at index 0 in this vector (without removing it). More...
 
const ValueType & front() const
 Returns the element at index 0 in this vector (without removing it). More...
 
const ValueType & get(int index) const
 Returns the element at the specified index in this vector. More...
 
int indexOf(const ValueType &value) const
 Returns the index of the first occurrence of the given value. More...
 
void insert(int index, const ValueType &value)
 Inserts the element into this vector before the specified index. More...
 
bool isEmpty() const
 Returns true if this vector contains no elements. More...
 
int lastIndexOf(const ValueType &value) const
 Returns the index of the last occurrence of the given value. More...
 
void mapAll(std::function< void(const ValueType &)> fn) const
 Calls the specified function on each element of the vector in ascending index order. More...
 
bool operator!=(const Vector &v2) const
 Compares two vectors for inequality. More...
 
Vector operator+(const Vector &v2) const
 Concatenates two vectors and returns the result. More...
 
Vector operator+(const ValueType &elem) const
 Produces a vector formed by appending the given element to this vector. More...
 
Vectoroperator+=(const Vector &v2)
 Adds all of the elements from v2 to the end of this vector. More...
 
Vectoroperator+=(const ValueType &value)
 Adds the single specified value) to the end of the vector. More...
 
Vectoroperator,(const ValueType &value)
 Adds an element to the vector passed as the left-hand operatand. More...
 
bool operator<(const Vector &v2) const
 Relational operators to compare two vectors. More...
 
bool operator<=(const Vector &v2) const
 Relational operators to compare two vectors. More...
 
bool operator==(const Vector &v2) const
 Compares two vectors for equality. More...
 
bool operator>(const Vector &v2) const
 Relational operators to compare two vectors. More...
 
bool operator>=(const Vector &v2) const
 Relational operators to compare two vectors. More...
 
ValueType & operator[](int index)
 Overloads [] to select elements from this vector. More...
 
const ValueType & operator[](int index) const
 Overloads [] to select elements from this vector. More...
 
ValueType pop_back()
 Removes and returns the last value of this vector. More...
 
ValueType pop_front()
 Removes and returns the first value of this vector. More...
 
void push_back(const ValueType &value)
 Adds a new value to the end of this vector. More...
 
void push_front(const ValueType &value)
 Adds a new value to the start of this vector. More...
 
void remove(int index)
 Removes the element at the specified index from this vector. More...
 
ValueType removeBack()
 Removes and returns the element at index (size - 1) in this vector. More...
 
ValueType removeFront()
 Removes and returns the element at index 0 in this vector. More...
 
void removeValue(const ValueType &value)
 Removes the first occurrence of the element value from this vector. More...
 
void reverse()
 Reverses the order of the elements in this vector. More...
 
void set(int index, const ValueType &value)
 Replaces the element at the specified index in this vector with a new value. More...
 
void shuffle()
 Rearranges the order of the elements in this vector into a random order. More...
 
int size() const
 Returns the number of elements in this vector. More...
 
void sort()
 Rearranges the order of the elements in this vector into sorted order. More...
 
Vector< ValueType > subList(int start, int length) const
 Returns a new vector containing the given subset range of elements from this vector. More...
 
Vector< ValueType > subList(int start) const
 Returns a new vector containing the elements from the start position to the end of the vector. More...
 
string toString() const
 Converts the vector to a printable string representation such as "{10, 20, 30, 40}". More...
 
void updateVersion()
 

Detailed Description

template<typename ValueType>
class Vector< ValueType >

This class stores an ordered list of values similar to an array.

It supports traditional array selection using square brackets, but also supports inserting and deleting elements. It is similar in function to the STL vector type, but is simpler both to use and to implement.

Member Typedef Documentation

◆ const_iterator

using const_iterator = stanfordcpplib::collections::CheckedIterator<typename ContainerType::const_iterator>

◆ iterator

using iterator = stanfordcpplib::collections::CheckedIterator<typename ContainerType::iterator>

Constructor & Destructor Documentation

◆ Vector() [1/3]

Vector ( )
default

Initializes a new empty vector.

Big-Oh:
O(1)

◆ Vector() [2/3]

Vector ( int  n,
ValueType  value = ValueType() 
)
explicit

Initializes a new vector, creating an array with n elements, each of which is initialized to value.

If value is missing, the elements are initialized to the default value for the type.

Big-Oh:
O(N)

◆ Vector() [3/3]

Vector ( std::initializer_list< ValueType >  list)

Uses an initializer list to set up the vector.

Big-Oh:
O(N)

◆ ~Vector()

virtual ~Vector ( )
virtualdefault

Frees any heap storage allocated by this vector.

Big-Oh:
O(1)

Member Function Documentation

◆ add()

void add ( const ValueType &  value)

Adds a new value to the end of this vector.

Big-Oh:
O(1)

◆ addAll()

Vector< ValueType > & addAll ( const Vector< ValueType > &  v)

Adds all elements of the given other vector to this vector.

Returns a reference to this vector. Identical in behavior to the += operator.

Big-Oh:
O(N)

◆ back() [1/2]

ValueType & back ( )

Returns the element at index (size - 1) in this vector (without removing it).

Exceptions
ErrorExceptionif vector is empty
Big-Oh:
O(1)

◆ back() [2/2]

const ValueType & back ( ) const

Returns the element at index (size - 1) in this vector (without removing it).

Exceptions
ErrorExceptionif vector is empty
Big-Oh:
O(1)

◆ begin() [1/2]

Vector< ValueType >::iterator begin ( )

◆ begin() [2/2]

Vector< ValueType >::const_iterator begin ( ) const

◆ clear()

void clear ( )

Removes all elements from this vector.

Big-Oh:
O(1)

◆ contains()

bool contains ( const ValueType &  value) const

Returns true if the vector contains the given value.

The ValueType must have an == operator to use this method.

Big-Oh:
O(N)

◆ end() [1/2]

Vector< ValueType >::iterator end ( )

◆ end() [2/2]

Vector< ValueType >::const_iterator end ( ) const

◆ ensureCapacity()

void ensureCapacity ( int  cap)

Guarantees that the vector's internal array is at least the given length.

If necessary, resizes the array to be the given length or larger.

Big-Oh:
O(N)

◆ equals()

bool equals ( const Vector< ValueType > &  v) const

Compares two vectors for equality.

Returns true if this vector contains exactly the same values as the given other vector. Identical in behavior to the == operator.

Big-Oh:
O(N)

◆ front() [1/2]

ValueType & front ( )

Returns the element at index 0 in this vector (without removing it).

Exceptions
ErrorExceptoinif vector is empty
Big-Oh:
O(1)

◆ front() [2/2]

const ValueType & front ( ) const

Returns the element at index 0 in this vector (without removing it).

Exceptions
ErrorExceptoinif vector is empty
Big-Oh:
O(1)

◆ get()

const ValueType & get ( int  index) const

Returns the element at the specified index in this vector.

Similar in behavior to the [] operator.

Exceptions
ErrorExceptionif the index is not in the array range
Big-Oh:
O(1)

◆ indexOf()

int indexOf ( const ValueType &  value) const

Returns the index of the first occurrence of the given value.

If the value is not found in the vector, returns -1. The ValueType must have an == operator to use this method.

Big-Oh:
O(N)

◆ insert()

void insert ( int  index,
const ValueType &  value 
)

Inserts the element into this vector before the specified index.

All subsequent elements are shifted one position to the right.

Exceptions
ErrorExceptionif the index is not in the array range from 0 up to and including the length of the vector.
Big-Oh:
O(N)

◆ isEmpty()

bool isEmpty ( ) const

Returns true if this vector contains no elements.

Big-Oh:
O(1)

◆ lastIndexOf()

int lastIndexOf ( const ValueType &  value) const

Returns the index of the last occurrence of the given value.

If the value is not found in the vector, returns -1. The ValueType must have an == operator to use this method.

Big-Oh:
O(N)

◆ mapAll()

void mapAll ( std::function< void(const ValueType &)>  fn) const

Calls the specified function on each element of the vector in ascending index order.

Big-Oh:
O(N)

◆ operator!=()

bool operator!= ( const Vector< ValueType > &  v2) const

Compares two vectors for inequality.

The ValueType must have a != operator.

Big-Oh:
O(N)

◆ operator+() [1/2]

Vector< ValueType > operator+ ( const Vector< ValueType > &  v2) const

Concatenates two vectors and returns the result.

Big-Oh:
O(N)

◆ operator+() [2/2]

Vector< ValueType > operator+ ( const ValueType &  elem) const

Produces a vector formed by appending the given element to this vector.

Big-Oh:
O(N)

◆ operator+=() [1/2]

Vector< ValueType > & operator+= ( const Vector< ValueType > &  v2)

Adds all of the elements from v2 to the end of this vector.

Big-Oh:
O(N)

◆ operator+=() [2/2]

Vector< ValueType > & operator+= ( const ValueType &  value)

Adds the single specified value) to the end of the vector.

Big-Oh:
O(1)

◆ operator,()

Vector< ValueType > & operator, ( const ValueType &  value)

Adds an element to the vector passed as the left-hand operatand.

This form makes it easier to initialize vectors in old versions of C++.

Big-Oh:
O(1)

◆ operator<()

bool operator< ( const Vector< ValueType > &  v2) const

Relational operators to compare two vectors.

Each element is compared pairwise to the corresponding element at the same index in the other vector; for example, we first check the values at index 0, then 1, and so on. The <, >, <=, >= operators require that the ValueType has a < operator so that the elements can be compared pairwise.

Big-Oh:
O(N)

◆ operator<=()

bool operator<= ( const Vector< ValueType > &  v2) const

Relational operators to compare two vectors.

Each element is compared pairwise to the corresponding element at the same index in the other vector; for example, we first check the values at index 0, then 1, and so on. The <, >, <=, >= operators require that the ValueType has a < operator so that the elements can be compared pairwise.

Big-Oh:
O(N)

◆ operator==()

bool operator== ( const Vector< ValueType > &  v2) const

Compares two vectors for equality.

The ValueType must have an == operator.

Big-Oh:
O(N)

◆ operator>()

bool operator> ( const Vector< ValueType > &  v2) const

Relational operators to compare two vectors.

Each element is compared pairwise to the corresponding element at the same index in the other vector; for example, we first check the values at index 0, then 1, and so on. The <, >, <=, >= operators require that the ValueType has a < operator so that the elements can be compared pairwise.

Big-Oh:
O(N)

◆ operator>=()

bool operator>= ( const Vector< ValueType > &  v2) const

Relational operators to compare two vectors.

Each element is compared pairwise to the corresponding element at the same index in the other vector; for example, we first check the values at index 0, then 1, and so on. The <, >, <=, >= operators require that the ValueType has a < operator so that the elements can be compared pairwise.

Big-Oh:
O(N)

◆ operator[]() [1/2]

ValueType & operator[] ( int  index)

Overloads [] to select elements from this vector.

This extension enables the use of traditional array notation to get or set individual elements.

Exceptions
ErrorExceptionif the index is not in the array range
Big-Oh:
O(1)

◆ operator[]() [2/2]

const ValueType & operator[] ( int  index) const

Overloads [] to select elements from this vector.

This extension enables the use of traditional array notation to get or set individual elements.

Exceptions
ErrorExceptionif the index is not in the array range
Big-Oh:
O(1)

◆ pop_back()

ValueType pop_back ( )

Removes and returns the last value of this vector.

Equivalent to removeBack.

Exceptions
ErrorExceptionif the vector is empty
Big-Oh:
O(1)

◆ pop_front()

ValueType pop_front ( )

Removes and returns the first value of this vector.

Equivalent to removeFront.

Exceptions
ErrorExceptionif the vector is empty
Big-Oh:
O(N)

◆ push_back()

void push_back ( const ValueType &  value)

Adds a new value to the end of this vector.

This method is a synonym of the add method that is provided to ensure compatibility with the STL vector class.

Big-Oh:
O(1)

◆ push_front()

void push_front ( const ValueType &  value)

Adds a new value to the start of this vector.

This method is equivalent to calling insert(0, value) and is provided to improve compatibility with the STL vector class.

Big-Oh:
O(N)

◆ remove()

void remove ( int  index)

Removes the element at the specified index from this vector.

All subsequent elements are shifted one position to the left.

Exceptions
ErrorExceptionif the index is not in the array range
Big-Oh:
O(N)

◆ removeBack()

ValueType removeBack ( )

Removes and returns the element at index (size - 1) in this vector.

Exceptions
ErrorExceptionif the vector is empty
Big-Oh:
O(1)

◆ removeFront()

ValueType removeFront ( )

Removes and returns the element at index 0 in this vector.

Exceptions
ErrorExceptionif the vector is empty
Big-Oh:
O(N)

◆ removeValue()

void removeValue ( const ValueType &  value)

Removes the first occurrence of the element value from this vector.

All subsequent elements are shifted one position to the left. If the vector does not contain the given value, has no effect. The ValueType must have an == operator to use this method.

Big-Oh:
O(N)

◆ reverse()

void reverse ( )

Reverses the order of the elements in this vector.

For example, if vector stores {1, 3, 4, 9}, changes it to store {9, 4, 3, 1}.

Big-Oh:
O(N)

◆ set()

void set ( int  index,
const ValueType &  value 
)

Replaces the element at the specified index in this vector with a new value.

The previous value at that index is overwritten. Similar in behavior to the [] operator.

Exceptions
ErrorExceptionif the index is not in the array range
Big-Oh:
O(1)

◆ shuffle()

void shuffle ( )

Rearranges the order of the elements in this vector into a random order.

Big-Oh:
O(N)

◆ size()

int size ( ) const

Returns the number of elements in this vector.

Big-Oh:
O(1)

◆ sort()

void sort ( )

Rearranges the order of the elements in this vector into sorted order.

For example, if vector stores {9, 1, 4, 3}, changes it to store {1, 3, 4, 9}. The ValueType must have an operator < to call this method.

Big-Oh:
O(N log N)

◆ subList() [1/2]

Vector< ValueType > subList ( int  start,
int  length 
) const

Returns a new vector containing the given subset range of elements from this vector.

The new vector is a deep copy, not linked to this one.

Exceptions
ErrorExceptionif the range (start .. start + length) is not within the bounds of this vector, or if length is negative
Big-Oh:
O(N)

◆ subList() [2/2]

Vector< ValueType > subList ( int  start) const

Returns a new vector containing the elements from the start position to the end of the vector.

Exceptions
ErrorExceptionif start > size()
Big-Oh:
O(N)

◆ toString()

string toString ( ) const

Converts the vector to a printable string representation such as "{10, 20, 30, 40}".

Big-Oh:
O(N)

◆ updateVersion()

void updateVersion ( )