go to start Ex W11
|home |print view |recent changes |changed November 29, 2018 |
|You are <- set your identity!

Sections: ''Testat-Exercise 3'': indexableSet class template | Binary not Found | 0 Introductory Questions | Static or Dynamic Errors? | Template Definition | Dependent Types | Inheritance | Template Specialization | 1 Indexable Set Class Template (3.TESTAT) | 2 A vector with backward indexing using negative indices | Optional: Extra Exercise (ACCU Student Code Critique 97) |

Testat-Exercise 3: indexableSet class template ^

Hand in time is Monday Dec 10 2018, 12:00 (noon) (CET)

Hand in all your source files attached to a single email to thomas.corbat@hsr.ch, peter.sommerlad@hsr.ch . NO ZIP, NO object files, NO eclipse project.

Binary not Found ^

0 Introductory Questions ^

Static or Dynamic Errors? ^

When will a template be instanciated?

Template Definition ^

Where do you define a class template and its members?

Dependent Types ^

If you have a member type of another type depending on a template parameter, how do you have to refer to that type?

Inheritance ^

How do you refer to members inherited from a dependent type within a class template?

Template Specialization ^

Is it neccessary for a (partial) specialization of a class template to provide the same interface (same member functions) as the unspecialized template?

1 Indexable Set Class Template (3.TESTAT) ^

Create a class template indexableSet<typename T, typename COMPARE=std::less<T>> that inherits from std::set<T,COMPARE> and allows indexing its content like you could do with a std::vector<T>. Negative indices should index from the "end" of the set. Provide also member functions front() and back() that correspond to those of vector.

Write Test cases and implementation code for indexableSet for the following aspects:


2 A vector with backward indexing using negative indices ^

The goal is to a) get familiar with the standard library's container interface and b) to exercise writing a template class.

Many scripting languages allow dynamic arrays to be indexed with negative indices where a[-1] denotes the last element in the arraya[size-1] and a[-size] the first element a[0] .

Create a template class dynArray<T> that uses a std::vector<T> as a member (not as a super class) for its implementation. Implement the indexing member functions similar to std::vector for dynArray in a way that negative indices are allowed and every index access is actually bounds checked (use std::vector's facility for that).

The following functions of std::vector you should not implement:

But you must implement all other members of std::vector by delegating to the corresponding member functions of your template class' data member of type std::vector.

Optional: Extra Exercise (ACCU Student Code Critique 97) ^

Can you help the following student with his C++ template problems?

I have a simple template class that holds a collection of values but sometimes code using the class crashes. I've written as simple test for the class, which works, but I do get a warning about a signed/unsigned mismatch on the for loop. I though using auto would stop that. Is that anything to do with the crash? I've commented out all the other methods apart from add and remove.

--- values.h ---

#include <utility>
#include <vector>

#pragma once

// An unordered collection of values
template <typename T>
class Values
  void add(T t);
  bool remove(T t);
  std::vector<T> const & values() const
  { return v; }
  std::vector<T> v;

// Add a new item
template <typename T>
void Values<T>::add(T t)

// Remove an item
// Returns true if removed, false if not present
template <typename T>
bool Values<T>::remove(T t)
  bool found(false);

  for (auto i = 0; i != v.size(); ++i)
    if (v[i] == t)
      v[i] = std::move(v.back());
      found = true;
  return found;
-- test.cpp --
#include <iostream>
#include "values.h"

void test()
  Values<int> vi;
  for (int i = 0; i != 10; ++i)

  if (!vi.remove(1))
     std::cout << "Can't remove 1\n";

  if (vi.remove(1))
     std::cout << "Can remove 1 twice\n";

  if (!vi.remove(9))
    std::cout << "Can't remove 9\n";
  std::cout << "size: " << vi.values().size()
    << std::endl;

int main()

|home |print view |recent changes |changed November 29, 2018 |
|You are <- set your identity!

Ex W11
go to start