Colorado State University

This file defines the header for each page. An optional "icon" image (I use the textbook):

A link to enable editing of this page: Edit TopBar Replace this with info about this class:

CS253: Problem Solving with C++

Spring 2012

HW 6

Links to the various pages for this class:

Wish I could do this: * Schedule

CS253 HW6: Shift-Over List

Description

For this assignment, you will implement a templated STL-like container called a Shift-Over List, or Sol.

You will turn in a single file, Sol.h. You may use C++11 features.

Template Parameters

An Sol takes three template parameters:

Examples

Here are several example programs:

    #include "Sol.h"
    #include <iostream>

    using namespace std;

    int main() {
        Sol<int> foo;

        foo.insert(2);
        foo.insert(5);
        foo.insert(3);

        // Should print "253"
        for (Sol<int>::iterator it = foo.begin(); it != foo.end(); ++it)
            cout << *it;
        cout << '\n';
        return 0;
    }

    #include "Sol.h"
    #include <iostream>
    #include <cassert>
    #include <cstring>
    #include <sstream>

    using namespace std;

    template<typename T>
    string cat(const T &con) {
        ostringstream os;
        for (typename T::iterator it = con.begin(); it != con.end(); ++it)
            os << *it;
        return os.str();
    }

    int main() {
        char data[] = "abcdefghXYZ";
        Sol<char,4> foo(data, data+strlen(data));
        Sol<char,4>::iterator it;

        foo.erase('X');
        cout << cat(foo) << '\n';   // Should print abcdefghYZ
        it = foo.find('Z');
        assert(it != foo.end());
        assert(*it == 'Z');
        cout << cat(foo) << '\n';   // Should print abcdeZfghY
        foo.find('Z');
        cout << cat(foo) << '\n';   // Should print aZbcdefghY
        foo.find('Z');
        cout << cat(foo) << '\n';   // Should print ZabcdefghY

        return 0;
    }

    #include "Sol.h"
    #include <iostream>
    #include <cassert>
    #include <cstring>
    #include <sstream>

    using namespace std;

    template<typename T>
    string cat(const T &con) {
        ostringstream os;
        for (typename T::iterator it = con.begin(); it != con.end(); ++it)
            os << *it;
        return os.str();
    }

    int main() {
        Sol<int> foo;
        for (int i=1; i<=9; i++)
            foo.insert(i);

        cout << cat(foo) << '\n';    // Should print 123456789
        cout << cat(foo+3) << '\n';  // Should print 789123456
        cout << cat(foo) << '\n';    // Should print 123456789
        ++foo;
        cout << cat(foo) << '\n';    // Should print 912345678
        foo -= 2;
        cout << cat(foo) << '\n';    // Should print 234567891

        return 0;
    }

    #include "Sol.h"
    #include <cassert>
    #include <iostream>
    #include <sstream>

    using namespace std;

    // A simple wrapper around a float.
    class Age {
      public:
        Age() : years(0.0) { }
        Age(float y) : years(y) { }
        float years;
    };

    // Note that you CAN'T compare two Age objects using < or ==.
    // This compares two Age objects, but only the integral portion.
    // Therefore, it is NOT the case that 5.3 compares less than 5.4;
    // they’re both the same as far as we’re concerned.
    class AgeCompare {
      public:
        bool operator()(Age a, Age b) const {
            return int(a.years) < int(b.years);
        }
    };

    int main() {
        Sol<Age, 0, AgeCompare> who;
        who.insert(1.25);   // a baby
        who.insert(54.45);  // Jack
        who.insert(20);     // a typical student

        assert(who.find(1.00) != who.end());   // should succeed
        assert(who.find(1.25) != who.end());   // should succeed
        assert(who.find(1.99) != who.end());   // should succeed
        assert(who.find(2.00) == who.end());   // should fail

        assert(who.find(53.45) == who.end());  // should fail
        assert(who.find(54.00) != who.end());  // should succeed
        assert(who.find(54.45) != who.end());  // should succeed
        assert(who.find(54.78) != who.end());  // should succeed
        assert(who.find(55.78) == who.end());  // should fail

        assert(who.find(19.99) == who.end());  // should fail
        assert(who.find(20.00) != who.end());  // should succeed
        assert(who.find(20.11) != who.end());  // should succeed
        assert(who.find(20.99) != who.end());  // should succeed
        assert(who.find(21.00) == who.end());  // should fail
        assert(who.find(21.01) == who.end());  // should fail

        assert(who.count(54.1) == 1);       // Are we using the functor?

        cout << "Success!  Promotions for everyone!\n";
    }

Requirements for stored type

The type stored by this templated class has the following requirements:

Required public types of Sol

The following types are required, and have similar meanings as they do in the standard STL containers:

Required public methods of Sol

Required operators

Required operations on Sol::iterator:

Requirements

Hints

How to submit your homework:

Follow the directions on the homework page.

How to receive negative points:

Turn in someone else’s work.


Page: Main.HW6
Modified: April 30, 2012, at 11:00 AM
Wiki: pmwiki-2.2.35
CS Department
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2011 Colorado State University