Search code examples
c++classtemplatesaccumulator

Applying Greater than or Less than Checks to both Strings and Primitive Data Types?


I'm working on a project which requires that I create a template class for an Accumulator which returns weather or not the list passed to it is in order. The order is ascending.

I am probably overthinking the problem but I cannot seem to figure out how to do a greater than/ less than check on both primitive data types and strings. I'll clarify: The process goes like this: A list/vector is declared and stuff is stored in it. Then a secondary Accumulator called apply is called. This Accumulator(apply) iterates through the list calling the .put() method of the InOrder Accumulator on every value in the list. The values could be of type double, long, short, etc or string.

I have tried setting an arbitrary lower bound, setting it equal to the first element in the list and then doing checks based on that start point but this offers mixed results because it does not work on the strings.

I was thinking of checking typeid or something so that for strings I could then call the .size() method and compare that way. Were as for primitives I would simply use the > or < operator. But that would defeat the point of the template function. Any help would be greatly appreciated.

I'll post the Code were the function is called, the code for the Apply Accumulator, and my Code for InOrder. Let me know if anything else is required.

My InOrder:

template<typename T>
class InOrder
{
public:

InOrder(){}
~InOrder(){}

void put(T item)
{
    _count++;
    if(_count == 1)
    {
        _lowbound = item;
    }
    if(_count!=0 && _count!=1 && item<_lowbound)
    {
        _order = false;
    }
   if(_count!=0 && _count!=1 && item>_lowbound)
   {
       _order = true;
   }
    _count++;
}
bool get()
{
    return _order;
}

private:
T _lowbound;
int _count = 0;
bool _order;
};

Apply Accumulator:

template<typename A, typename I>
void apply(A & anAccumulator, I begin, I end)
{
for (I iter = begin; iter != end; ++iter)
{
    anAccumulator.put( *iter);
}
}

Code where InOrder is Called:

{
    // Read a list of doubles into a List and check their order
    cout << "apply InOrder to a List of doubles\n";
    double sentinel = -1.23;
    List<double> dList;
    fillList(sentinel, dList);
    InOrder<double> dblInOrder;
    apply(dblInOrder, begin(dList), end(dList));
    cout << "The doubles in dList are ";
    if (!dblInOrder.get())
        cout << "NOT ";
    cout << "in order\n\n";
}
{
    // Read a list of strings into a List and check their order
    cout << "apply InOrder to a List of strings\n";
    string strSent = "end";
    List<string> sList;
    fillList(strSent, sList);
    InOrder<string> strInOrder;
    apply(strInOrder, begin(sList), end(sList));
    cout << "The strings in sList are ";
    if (!strInOrder.get())
        cout << "NOT ";
    cout << "in order\n\n";
}

I should note that the the items put into the list are processed in the reverse order.
Eg: if I type my list in as [a,b,c] or [1,2,3] the first value/string to be processed will be c/3 and then so on for there to b/2 and a,1


Solution

  • Your mistake is that you do not stop when you detect that order is wrong:

     _order = false; // here is you have to stop and skip all other items