You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 24, 2019. It is now read-only.
#0.7 The Template Container (templates, iterators, ...)
The previously implemented class UIntVector serves its purpose, but what if
we would like to store an arbitrary type T in a similar container, without
the need to write a new implementation from scratch?
Your task is to write a class template that, when instantiated as Vector<T>,
yields a type that can store any arbitrary number of elements of type T.
0.7.1 Requirements
Your class template implementation shall satisfy the requirements of UIntVector, as
well as the following:
The class template shall be able to be instantiated as Vector, where T denotes
the type of the contained elements.
It shall not be possible to instantiate the class template unless the specified element
type is both MoveConstructible and MoveAssignable.
You shall use static_assert, with an appropriate error message, to make sure
that this is the case.
Additional initialization functionality:
It shall be possible to default-construct the container, which shall be seman-
tically equivalent to Vector<T> (0).
It shall be possible to construct an instance of the container by passing the
initial number of elements, as well as the initial value for those elements, as
in; Vector<float> (10, 3.14f).
The following member-functions shall be implemented.
void push_back(T) - Appends the given element value to the end of the
container with amortized constant time complexity, meaning that
insertions are constant in most cases.
void insert(std::size_t, T) - Inserts the element value immediately
before the index specified. If index == size(), see push_back. If the
index is out-of-bounds; throw a std::out_of_range.
void clear() - Removes every element, effectively making size() == 0.
void erase(std::size_t) - Removes the element at the index specified.
std::size_t size() - Returns the number of elements in the container.
std::size_t capacity() - Returns the number of elements that can
potentially be stored in the container without having to reallocate the
underlying storage.
unspecified begin() - Returns a RandomAccessIterator to the first ele-
ment of the range.
unspecified end() - Returns a RandomAccessIterator referring to the
element right after the last element in the container.
unspecified find(T const&) - Returns a RandomAccessIterator referring
to the first element that compares equal to the argument, or end() if no
such element is found.
0.7.2 Hints
Make sure that you mark the appropriate member-functions as const.
<type_traits> contains traits [6] that can be used to check whether a
certain type has some characteristic, such as to see if a type T is MoveConstructible.
There is a simple cxxtest, test_template_vec.cpp, available in the lab
directory, which intentionally is somewhat incomplete.
You should further increase its functionality to make sure that your
implementation satisfies the requirements of this assignment.
0.7.3 Questions
Iterating over a range of elements can be done with a range-based for-loop, but the
type of source must meet certain requirements; what are they?
for (auto const& elem : source) {
...
}
The C++ Standard sometimes state that a type in the Standard Library is unspecified; why do you think that is?
20.11.7.3 Class high_resolution_clock [time.clock.hires]
Objects of class high_resolution_clock represent clocks with the shortest
tick period. high_resolution_clock may be a synonym for system_clock or steady_clock.
#0.7 The Template Container (templates, iterators, ...)
The previously implemented class
UIntVector
serves its purpose, but what ifwe would like to store an arbitrary type
T
in a similar container, withoutthe need to write a new implementation from scratch?
Your task is to write a class template that, when instantiated as
Vector<T>
,yields a type that can store any arbitrary number of elements of type
T
.0.7.1 Requirements
Your class template implementation shall satisfy the requirements of
UIntVector
, aswell as the following:
the type of the contained elements.
type is both
MoveConstructible
andMoveAssignable
.You shall use
static_assert
, with an appropriate error message, to make surethat this is the case.
tically equivalent to
Vector<T> (0)
.initial number of elements, as well as the initial value for those elements, as
in;
Vector<float> (10, 3.14f)
.void push_back(T)
- Appends the given element value to the end of thecontainer with amortized constant time complexity, meaning that
insertions are constant in most cases.
void insert(std::size_t, T)
- Inserts the element value immediatelybefore the index specified. If
index == size()
, seepush_back
. If theindex is out-of-bounds; throw a
std::out_of_range
.void clear()
- Removes every element, effectively makingsize() == 0
.void erase(std::size_t)
- Removes the element at the index specified.std::size_t size()
- Returns the number of elements in the container.std::size_t capacity()
- Returns the number of elements that canpotentially be stored in the container without having to reallocate the
underlying storage.
unspecified begin()
- Returns aRandomAccessIterator
to the first ele-ment of the range.
unspecified end()
- Returns aRandomAccessIterator
referring to theelement right after the last element in the container.
unspecified find(T const&)
- Returns aRandomAccessIterator
referringto the first element that compares equal to the argument, or
end()
if nosuch element is found.
0.7.2 Hints
Make sure that you mark the appropriate member-functions as
const
.<type_traits>
contains traits [6] that can be used to check whether acertain type has some characteristic, such as to see if a type
T
isMoveConstructible.
There is a simple cxxtest,
test_template_vec.cpp
, available in the labdirectory, which intentionally is somewhat incomplete.
You should further increase its functionality to make sure that your
implementation satisfies the requirements of this assignment.
0.7.3 Questions
Iterating over a range of elements can be done with a range-based for-loop, but the
type of source must meet certain requirements; what are they?
The C++ Standard sometimes state that a type in the Standard Library is
unspecified; why do you think that is?
Objects of class
high_resolution_clock
represent clocks with the shortesttick period.
high_resolution_clock
may be a synonym forsystem_clock
orsteady_clock
.[6]: a type trait is an entity which can be used to query characteristic of any arbitrary type
U
duringcompile-time.
The text was updated successfully, but these errors were encountered: