GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


A Paste by Anonymous
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
#include \"Massert.h\"
#pragma comment(lib, \"massert.lib\")
*/

template< class _T >
void displayvect( vector<_T>& v ) {
    for( unsigned x = 0; x < v.size(); ++x )
        cout << v[x] << \" \" << endl;
    cout << endl;
}

enum SortMode { NONE, INT, DOUBLE, CHAR };

class sorttest {
    int x;
    double y;
    char g;
    static SortMode sm[3];

    bool intersort( SortMode s, sorttest& t, int ind = 0 ) {
        if( (ind < 3) && sm[ind] ) {
            switch( s ) {
            case INT: 
                if( x == t.x )
                    return intersort( sm[ind+1], t, ind+1 );
                return x < t.x;
            case DOUBLE: 
                if( y == t.y )
                    return intersort( sm[ind+1], t, ind+1 ); 
                return y < t.y;
            case CHAR: 
                if( g == t.g ) 
                    return intersort( sm[ind+1], t, ind+1 ); 
                return g < t.g;
            }
        }
        // gets here if all 3 are ==
        return true;
    }
public:
    sorttest( int i, double f, char c ) : x(i), y(f), g(c) {
        sm[0] = INT; sm[1] = NONE; sm[2] = NONE;        
    }
    bool operator<( sorttest& t ) {
        return intersort( sm[0], t );
    }
    static void setSort( SortMode s1, SortMode s2 = NONE, SortMode s3 = NONE ) { sm[0] = s1; sm[1] = s2; sm[2] = s3; }

    friend ostream& operator<<( ostream& os, sorttest& st );
};

// initalize static member
SortMode sorttest::sm[3];

ostream& operator<<( ostream& os, sorttest& st ) {
    os << \"(\" << st.x << \", \" << st.y << \", \" << st.g << \")\";
    return os;
}

int main() {
    vector<sorttest> testv;
    testv.push_back( sorttest( 9, 8.5, \'a\' ) );
    testv.push_back( sorttest( 9, 6.7, \'a\'  ) );
    testv.push_back( sorttest( 9, 6.3, \'a\'  ) );
    testv.push_back( sorttest( 3, 7.9, \'b\'  ) );
    testv.push_back( sorttest( 3, 3.7, \'b\'  ) );
    testv.push_back( sorttest( 1, 7.9, \'c\'  ) );
    testv.push_back( sorttest( 2, 6.7, \'a\'  ) );

    displayvect( testv );

    cout << \"CHAR, INT, DOUBLE\" << endl;
    sorttest::setSort( CHAR, INT, DOUBLE );
    sort( testv.begin(), testv.end() );
    displayvect( testv );

    cout << \"INT, DOUBLE\" << endl;
    sorttest::setSort( INT, DOUBLE );
    sort( testv.begin(), testv.end() );
    displayvect( testv );

    cout << \"DOUBLE, CHAR, INT\" << endl;
    sorttest::setSort( DOUBLE, CHAR, INT );
    sort( testv.begin(), testv.end() );
    displayvect( testv );

    cout << \"CHAR\" << endl;
    sorttest::setSort( CHAR );
    sort( testv.begin(), testv.end() );
    displayvect( testv );    
    
    return 0;
}