[ create a new paste ] login | about

Link: http://codepad.org/bwaHcWyC    [ raw code | output | fork ]

Isoparametric - C++, pasted on Sep 28:
class Widget {
public:
    Widget(int value) : value_(value) {}
    int Value() const { return value_; }
private:
    int value_;
};

template<typename T>
class ValueThreshold : public unary_function<Widget, bool> {
private:
    const T threshold_;
public:
    ValueThreshold(const T& threshold) : threshold_(threshold) {}
    bool operator() (const Widget& widget) const {
        return widget.Value() >= threshold_;
    }
};

int main(int argc, char* argv[])
{
    Widget w1(500);
    Widget w2(30);
    Widget w3(2000);
    Widget w4(50);
    Widget w5(1000);
    
    list<Widget> widgets;
    
    widgets.push_back(w1);
    widgets.push_back(w2);
    widgets.push_back(w3);
    widgets.push_back(w4);
    widgets.push_back(w5);
    
    
    list<Widget>::reverse_iterator it = find_if(widgets.rbegin(), widgets.rend(),
        not1(ValueThreshold<int>(50)));
    if (it != widgets.rend()) {
        cout << (*it).Value() << endl;
    }

    list<Widget>::reverse_iterator it2 = find_if(widgets.rbegin(), widgets.rend(),
       ValueThreshold<int>(1500));
    if (it2 != widgets.rend()) {
        cout << (*it2).Value() << endl;
    }
    return 0;
}


Output:
1
2
30
2000


Create a new paste based on this one


Comments: