Computer/C/C++

STL lower_bound function 사용하기 Sample...

찬호 2007. 2. 27. 19:42
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <vector>
#include <ostream>
#include <list>
#include <iterator>
using namespace std;

class DataTuple {
private:
    int sourceID;
    int value;
    int high;
public:
    DataTuple();
    DataTuple(int sourceID, int value) {
        this->sourceID = sourceID;
        this->value = value;
        this->high = value+10;
    }
    bool operator<(const int &a) const
    {
        return (value<=a);
    }
   
   
    // Getters and Setters
    int getSourceID() {return sourceID;}
    void setSourceID(int sourceID) {this->sourceID = sourceID;}
    int getValue() {return value;}
    void setValue(float value) {this->value = value;}
};
DataTuple::DataTuple(){
    ;
}

class Something{
    public:
        bool operator()(DataTuple a, DataTuple value)  {
            return ( a.getValue() <= value.getValue() );
        }
};

int main()
// illustrate the use of the generic set algorithms
{
     list<DataTuple> a;
     list<DataTuple>::iterator ait, bit;
    
     DataTuple c1 = DataTuple(1,1);
     DataTuple c2 = DataTuple(2,11);
     DataTuple c3 = DataTuple(3,21);
     DataTuple c4 = DataTuple(4,31);
     a.push_back(c1);
     a.push_back(c2);
     a.push_back(c3);
     a.push_back(c4);

     int aaa = 31;

     DataTuple sample1;
     sample1.setValue(30);
     bit = --lower_bound(a.begin(),a.end(),sample1,Something());
    
     cout<<bit->getSourceID()<<endl;
     return 1;
}

compare operator에 const같은 것을 안쓰긴 했지만 아무튼...
이런식으로 사용된다.