set comparison method for class variable

Discussion in 'C++' started by jcteague@gmail.com, Oct 30, 2005.

  1. Guest

    I have a class that has a set<int> member variable. the items in the
    set are selected indices of a vector. I would like to sort the set on
    the values in the vector they point too. Here is a part of the header
    file:

    Class A{
    public:
    int getX(int i) //basic getter. gets value from vector

    private
    set<int> selectedElements;
    vector<int> x
    };

    I have tried several variations with a set comparison function with no
    luck. Any help would be greatly appreciated.

    thanks,
    John
    , Oct 30, 2005
    #1
    1. Advertising

  2. wrote:
    > I have a class that has a set<int> member variable. the items in the
    > set are selected indices of a vector. I would like to sort the set on
    > the values in the vector they point too. Here is a part of the header
    > file:
    >
    > Class A{
    > public:
    > int getX(int i) //basic getter. gets value from vector
    >
    > private
    > set<int> selectedElements;
    > vector<int> x
    > };
    >
    > I have tried several variations with a set comparison function with no
    > luck. Any help would be greatly appreciated.
    >
    > thanks,
    > John
    >


    It sounds a bit risky, what if the vector changes, then your set would
    be out of order with undefined effects.

    But in any case, you can't use a function for this purpose, you need a
    functor, something like this should do

    class Comp
    {
    public:
    Comp(A* p) : ptr(p) {}
    bool operator()(int a, int b) const
    {
    return p->getX(a) < p->getX(b);
    }
    private:
    A* ptr;
    };

    class A
    {
    public:
    A() : selectedElements(Comp(this)) {}
    private:
    set<int, Comp> selectedElements;
    vector<int> x;
    };

    Untested code.

    john
    John Harrison, Oct 30, 2005
    #2
    1. Advertising

  3. Here's a cleaned up version of the above code

    class A;

    class Comp
    {
    public:
    Comp(A* p) : ptr(p) {}
    bool operator()(int a, int b) const;
    private:
    A* ptr;
    };

    class A
    {
    public:
    A() : selectedElements(Comp(this)) {}
    int getX(int i) { return x; }
    private:
    set<int, Comp> selectedElements;
    vector<int> x;
    };

    inline bool Comp::eek:perator()(int a, int b) const
    {
    return ptr->getX(a) < ptr->getX(b);
    }
    John Harrison, Oct 30, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. John Black
    Replies:
    2
    Views:
    368
    jeffc
    Jun 15, 2004
  3. Pawel_Iks
    Replies:
    3
    Views:
    530
    terminator
    Jul 31, 2007
  4. Replies:
    3
    Views:
    925
    Roedy Green
    Jan 28, 2008
  5. Deepu
    Replies:
    1
    Views:
    236
    ccc31807
    Feb 7, 2011
Loading...

Share This Page