go to start Ex W12
|home |print view |recent changes |changed September 15, 2017 |
exact
|You are 54.81.59.211 <- 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 | Extra Exercise (ACCU Student Code Critique 97) |

Testat-Exercise 3: indexableSet class template ^

Hand in time is Monday Dec 18 2017, 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 ^

If you encouter the problem that Cevelop reports Binary not found even though the executable project has been built correctly, you might need to (re)activate the Binary Parsers for the project: Properties -> C/C++ Build -> Settings -> Binary Parsers (Tab) Actually, all required Binary Parsers should already be ticked. Remove the ticks and set them again. Press Apply. In your project an entry called Binaries should appear. Otherwise, ask your supervisor.

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 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 as the unspecialized template?

1 Indexable Set Class Template (3.TESTAT) ^

Create a class template indexableSet<T,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:

Note: Implement the template class indexableSet as header-only (indexableSet.h) within your CUTE test project.

Note: remember the rules for class templates inheriting from a class that also depends on a template parameter! Otherwise interesting effects for name lookup might happen. Omitting that will cause you to fail!

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.


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
{
public:
  void add(T t);
  bool remove(T t);
  std::vector<T> const & values() const
  { return v; }
private:
  std::vector<T> v;
};

// Add a new item
template <typename T>
void Values<T>::add(T t)
{
   v.push_back(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());
      v.pop_back();
      found = true;
    }
  }
  return found;
}
-- test.cpp --
#include <iostream>
#include "values.h"

void test()
{
  Values<int> vi;
  for (int i = 0; i != 10; ++i)
  {
    vi.add(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()
{
  test();
}


|home |print view |recent changes |changed September 15, 2017 |
exact
|You are 54.81.59.211 <- set your identity!

Ex W12
go to start