Skip to content
This repository has been archived by the owner on Feb 24, 2019. It is now read-only.

0.7 The Template Container (templates, iterators, ...) #19

Closed
emlun opened this issue Sep 18, 2014 · 1 comment
Closed

0.7 The Template Container (templates, iterators, ...) #19

emlun opened this issue Sep 18, 2014 · 1 comment

Comments

@emlun
Copy link
Collaborator

emlun commented Sep 18, 2014

#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.

    class high_resolution_clock {
    public:
      typedef unspecified                                             rep;
      typedef ratio<unspecified, unspecified>                         period;
      typedef chrono::duration<rep, period>                           duration;
      typedef chrono::time_point<unspecified, duration>               time_point;
      static const bool is_ready =                                    unspecified;
    
      static time_point now()                                         noexcept;
    };
    

[6]: a type trait is an entity which can be used to query characteristic of any arbitrary type U during
compile-time.

@emlun emlun added this to the Lab1 milestone Sep 18, 2014
@emlun emlun added the kattis label Sep 26, 2014
@emlun
Copy link
Collaborator Author

emlun commented Sep 26, 2014

Approved by Kattis in b7c5926

@emlun emlun closed this as completed Sep 26, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant