vector
overcomes the limitations of array
std::vector
API¶Defined in header <vector>
namespace std {
template <
typename T, // value type to store
typename Allocator = std::allocator<T> // how to allocate data
> class vector;
}
std::vector
Special Member Datatypes¶Name | Description |
---|---|
T |
template parameter for value type to store |
value_type |
alias to T |
size_type |
unsigned integer type (usually std::size_t ) |
difference_type |
signed integer type (usually std::ptrdiff_t ) |
reference |
alias to value_type& |
const_reference |
alias to value_type const& |
pointer |
usually just value_type* |
const_pointer |
usually just value_type const* |
iterator |
aliased to value_type* |
const_iterator |
aliased to value_type const* |
reverse_iterator |
std::reverse_iterator<iterator> |
const_reverse_iterator |
std::reverse_iterator<const_iterator> |
std::vector
Contructors¶vector ();
vector (size_type n);
vector (size_type n, T const& value);
template <typename InputIterator>
vector (InputIterator first, InputIterator last);
vector (std::initializer_list<T>);
std::vector
Access Operations¶reference back ();
reference front ();
reference operator[] (size_type i);
reference at (size_type i);
pointer data () noexcept;
std::vector
Access Operations (const
versions)¶const_reference back () const;
const_reference front () const;
const_reference operator[] (size_type i) const;
const_reference at (size_type i) const;
const_pointer data () const noexcept;
reference back ();
const_reference back() const;
const
version of back()
reference operator[] (size_type i);
reference at (size_type i);
operator[]
but does run-time bounds checkingstd::vector
Capacity Operations¶bool empty () const;
size_type size () const;
size_type capacity () const;
size_type max_size () const;
void shrink_to_fit ();
void reserve (size_type newcap);
bool empty() const;
true
IFF the vector's size is zero
size_type size () const
size_type capacity() const;
void shrink_to_fit();
capacity()
to size()
void reserve(size_type newcap);
capacity()
then new storage is allocated; otherwise, the method does nothingcapacity()
$\ge newcap$, size is unchangedstd::vector
Iterator Operations¶iterator begin () noexcept;
iterator end () noexcept;
reverse_iterator rbegin () noexcept;
reverse_iterator rend () noexcept;
const_iterator begin () const noexcept; // and cbegin()
const_iterator end () const noexcept; // and cend()
const_reverse_iterator rbegin () const noexcept; // and crbegin()
const_reverse_iterator rend () const noexcept; // and crend()
std::vector
Modifying Operations¶void clear();
void pop_back ();
iterator erase (...); // 2 versions
iterator insert (...); // 5 versions
void push_back (const_reference value); // and "move" variant
template <typename ... Args>
iterator emplace (const_iterator pos, Args&& ... value);
template <typename ... Args>
void emplace_back (Args&& ... value);
void resize(...); // 2 versions
void push_back (const_reference value);
void pop_back ();
iterator insert (iterator pos, const_reference value);
iterator erase (iterator pos);
iterator erase (const_iterator first, const_iterator last);
[first, last)
std::distance(first, last)
; the same number of elements are removed
void resize(size_type n);
T()
is filled for the increase in elements at the end. If the size is decreased, the original values at the front are retained.template <typename T>
class Vector {
// private members:
size_t m_size;
size_t m_capacity;
T* m_array;
};
// Default Constructor
Vector()
// Size Constructor
Vector(size_t pSize, const T& value = T())
// Range Constructor *
Vector(Iterator first, Iterator last)
// Copy Constructor **
Vector(const Vector& v)
// Destructor
~Vector
// Copy Assign *
Vector& operator=(const Vector& v)
// Indexing Operator *
T& operator[](size_t index)
// ensures that capacity() >= space *
void reserve (size_t space);
// ensures size() == newSize *
// - if newSize < size(), remove elements from end
// - if newSize > size(), insert copies of 'value' at end
void resize (size_t newSize, const T& value = T());
// adds a single element to the end *
void push_back(const T& item);
// removes a single element from the end *
void pop_back();
// inserts "item" before "pos" in the vector
// NOTE: must shift all elements from [pos, end()) right *
iterator insert (iterator pos, const T& item);
// removes element referred to by "pos" from the vector
// returns iterator to the element that comes after
// NOTE: must shift all elements from [pos + 1, end()) left **
iterator erase (iterator pos);
Implement the following Vector functions:
template <typename T>
class Vector {
public:
// Copy Constructor
Vector (const Vector& v);
// Range Constructor
Vector(const_iterator first, const_iterator last);
// Resize Operation
void resize (size_t newSize, const T& value = T());
// Erase
iterator erase (iterator pos);
};
Include your name; separate each function with whitespace; include comments where appropriate