comparator

Discussion in 'C++' started by bil050, Nov 29, 2010.

  1. bil050

    bil050 Guest

    Hi,
    I want to write comparator class,which compares int.These int are ID
    in some other class container and in order to compare them - I want to
    use these id just to retrieve appropriate objects from container and
    make comparison based on object data
    How can I do it?
    Thanks
     
    bil050, Nov 29, 2010
    #1
    1. Advertising

  2. bil050

    SG Guest

    On 29 Nov., 11:17, bil050 wrote:
    > I want to write comparator class,which compares int.These int are ID
    > in some other class container and in order to compare them - I want to


    Then, just do it. What's the problem?

    struct compare_ids
    {
    bool operator()(int a, int b) const {
    return ...;
    }
    };

    > use these id just to retrieve appropriate objects from container  and
    > make comparison  based on object data


    Then, you have to store a pointer or reference to this container
    object in the comparator object as data member so you know where to
    look things up in the operator() function.

    Cheers!
    SG
     
    SG, Nov 29, 2010
    #2
    1. Advertising

  3. bil050

    bil050 Guest

    On Nov 29, 12:38 pm, SG <> wrote:
    > On 29 Nov., 11:17, bil050 wrote:
    >
    > > I want to write comparator class,which compares int.These int are ID
    > > in some other class container and in order to compare them - I want to

    >
    > Then, just do it. What's the problem?
    >
    >   struct compare_ids
    >   {
    >     bool operator()(int a, int b) const {
    >       return ...;
    >     }
    >   };
    >
    > > use these id just to retrieve appropriate objects from container  and
    > > make comparison  based on object data

    >
    > Then, you have to store a pointer or reference to this container
    > object in the comparator object as data member so you know where to
    > look things up in the operator() function.
    >
    > Cheers!
    > SG


    Can you please give an example how I store the pointer?(In
    constructor?)
    I use this comparator for set.(i.e. set<int,compare_ids>)
    Thank you!
     
    bil050, Nov 29, 2010
    #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. Valentin Tihomirov

    flags vs. comparator

    Valentin Tihomirov, Nov 10, 2003, in forum: VHDL
    Replies:
    5
    Views:
    649
    Valentin Tihomirov
    Nov 11, 2003
  2. sunil
    Replies:
    4
    Views:
    784
    Ralf Hildebrandt
    Feb 22, 2004
  3. sk

    comparator problem

    sk, Nov 3, 2004, in forum: VHDL
    Replies:
    0
    Views:
    849
  4. john

    counter plus comparator

    john, Nov 8, 2004, in forum: VHDL
    Replies:
    4
    Views:
    773
    Raghavendra
    Nov 10, 2004
  5. Replies:
    6
    Views:
    516
Loading...

Share This Page