Resources‎ > ‎Programming‎ > ‎C++‎ > ‎C++ Tutorial‎ > ‎

Lesson 10

Practice 1 (STL)

First, use the following integer array,

int myarray[] = {435, 225, 55, 983, 100, 391, 355, 6, 26};
int size = sizeof myarray / sizeof(int);

write a code to store these integer values in a vector object. Useassign() function to copy the contents of myarray. Use iterator to display the integers stored in the vector.

Note: If you've successfully finished displaying the contents of the vector using vector's iterator, try using ostream_iterator to display all the elements in the vector.

Practice 2

use sort() function in <algorithm> to sort this vector. After sorting it, display sorted integers.

Practice 3

apply reverse() function to reverse the order and display the result.

Practice 4

In a lecture, we covered design patterns, which can be used to implement Undo/Redo function. There are different design patterns you can apply in order to achieve this (such as "Command" and "Memento" design patterns).

The following is a simple code to emulate a spread sheet and a cell. It uses STL's container class: list to store cells and a group of cells, and manipulate the current index number to achieve undo/redo function. Although you can see the use of list's iterator, the code simply uses the iterator and doesn't implement "Iterator Design Pattern". To "use" the "Iterator Design Pattern", your class needs to have appropriate function to provide your class's iterator (just like you've done in Week 9, practice 4).

In this practice, you need to apply "Memento" design pattern to the following code.

Hint:

Cell class -> keep it as is
Memento class -> you need to create one to store the state.
Sheet -> Originator and Caretaker
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
using namespace std;

class Cell {
    int x, y;
    string text;
public:
    Cell(int x, int y, string text) : x(x), y(y), text(text) {}
    int getX() {return x;}
    int getY() {return y;}
    string getText() {return text;}
    void setText(string text) {this->text = text;}
    Cell* clone() {return new Cell(x, y, text);}
};

class Sheet {
    list<list<Cell*>*> stack;
    int current;
    void addStack() {
        while (isRedoable()) {
            list<list<Cell*>*>::iterator itCells = stack.begin();
            advance(itCells, (stack.size() - 1));

            list<Cell*>::iterator itCell = (*itCells)->begin();
            while (itCell != (*itCells)->end()) {
                delete *itCell;
                itCell++;
            }
            delete *itCells;
            stack.erase(itCells);
        }

        list<Cell*>* pNewCells = new list<Cell*>;
        list<Cell*>::iterator itCell = getList()->begin();
        while (itCell != getList()->end()) {
            pNewCells->push_back((*itCell)->clone());
            itCell++;
        }
        stack.push_back(pNewCells);
        current++;
    }
    bool isRedoable() { return current < (int)stack.size() - 1;}
public:
    Sheet() : current(0) { stack.push_back(new list<Cell*>);}
    string getCellText(int x, int y) {
        Cell* pCell = getCell(x, y);
        return (pCell != NULL) ? pCell->getText() : NULL;
    }

    Cell* getCell(int x, int y) {
        list<Cell*>::iterator it = getList()->begin();
        while (it != getList()->end()) {
            Cell* pCell = *it;
            if ((pCell->getX() == x) && (pCell->getY() == y))
                return pCell;
            it++;
        }
        return NULL;
    }

    list<Cell*>* getList() {
        list<list<Cell*>*>::iterator it = stack.begin();
        for (int i = 0; it != stack.end(); it++, i++)
            if (current == i) return *it;
        return NULL;
    }

    void setCellText(int x, int y, string text) {
        addStack();
        Cell* pCell = getCell(x, y);
        if (pCell != NULL) pCell->setText(text);
        else getList()->push_back(new Cell(x, y, text));
    }

    void undo() { if (current > 0) current--;}
    void redo() { if (isRedoable()) current++;}
    void display() {
        list<Cell*>::iterator it = getList()->begin();
        while (it != getList()->end()) {
            cout << "x: " << (*it)->getX() << " ";
            cout << "y: " << (*it)->getY() << " ";
            cout << "Text: " << (*it)->getText() << endl;
            it++;
        }
    }
};

int main(){
    Sheet sheet;
    sheet.setCellText(1,1,"t1");
    sheet.setCellText(3,4,"t2");
    sheet.setCellText(3,4,"t3");
    sheet.setCellText(5,5,"t4");
    sheet.display();
    sheet.undo();
    sheet.undo();
    sheet.display();
    sheet.redo();
    sheet.display();
}

Subpages (1): Sample Solutions
Comments