Online Book Reader

Home Category

Professional C__ - Marc Gregoire [207]

By Root 1255 0

}

Error ErrorCorrelator::getError() throw(out_of_range)

{

// If there are no more errors, throw an exception.

if (mErrors.empty()) {

throw out_of_range("No elements!");

}

// Save the top element.

Error top = mErrors.top();

// Remove the top element.

mErrors.pop();

// Return the saved element.

return top;

}

Code snippet from ErrorCorrelatorPqueue\ErrorCorrelator.cpp

Here is a simple unit test showing how to use the ErrorCorrelator. Realistic use would require multiple threads so that one thread adds errors, while another processes them. As mentioned earlier with the queue example, without explicit synchronization provided by the programmer, no STL class can be used safely in multiple threads. Synchronization is discussed in Chapter 22.

ErrorCorrelator ec;

ec.addError(Error(3, "Unable to read file"));

ec.addError(Error(1, "Incorrect entry from user"));

ec.addError(Error(10, "Unable to allocate memory!"));

while (true) {

try {

Error e = ec.getError();

cout << e << endl;

} catch (const out_of_range&) {

cout << "Finished processing errors" << endl;

break;

}

}

Code snippet from ErrorCorrelatorPqueue\ErrorCorrelatorTest.cpp

The output of this program is as follows:

Unable to allocate memory! (priority 10)

Unable to read file (priority 3)

Incorrect entry from user (priority 1)

Finished processing errors

stack

The stack is almost identical to the queue, except that it provides first-in, last-out (FILO) semantics, also known as last-in, first-out (LIFO), instead of FIFO. It is defined in the header file. The template definition looks like this:

template > class stack;

You can use vector, list, or deque as the underlying model for the stack.

stack Operations

Like the queue, the stack provides push(), emplace() and pop(). The difference is that push() adds a new element to the top of the stack, “pushing down” all elements inserted earlier, and pop() removes the element from the top of the stack, which is the most recently inserted element. The top() method returns a const reference to the top element if called on a const object and a non-const reference if called on a non-const object.

pop() does not return the element popped. If you want to retain a copy, you must first retrieve it with top().

The stack supports empty(), size(), swap() and the standard comparison operators. See the Standard Library Reference resource on the website for details.

stack Example: Revised Error Correlator

Suppose that you decide to rewrite the previous ErrorCorrelator class so that it gives out the most recent errors instead of those with the highest priority. You can substitute a stack for the priority_queue in the ErrorCorrelator class definition. Now, the Errors will be distributed from the class in LIFO order instead of priority order. Nothing in the method definitions needs to change because the push(), pop(), top(), and empty() methods exist on both the priority_queue and stack. There is only one change required and only in the ErrorCorrelator class.

// Simple ErrorCorrelator class that returns most recent errors first.

class ErrorCorrelator

{

public:

ErrorCorrelator() {}

// Add an error to be correlated.

void addError(const Error& error);

// Retrieve the next error to be processed

Error getError() throw(std::out_of_range);

protected:

std::stack mErrors;

};

Code snippet from ErrorCorrelatorStack\ErrorCorrelator.h

All the rest of the code remains the same. The output of this stack version is as follows:

Unable to allocate memory! (priority 10)

Incorrect entry from user (priority 1)

Unable to read file (priority 3)

Finished processing errors

ASSOCIATIVE CONTAINERS


Unlike the sequential containers, the associative containers do not store elements in a linear configuration. Instead, they provide a mapping of keys to values. They generally offer insertion, deletion, and lookup times that are equivalent to each other.

The four associative containers provided by the STL are map, multimap, set, and multiset. Each of these containers

Return Main Page Previous Page Next Page

®Online Book Reader