Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > set comparison method for class variable

Reply
Thread Tools

set comparison method for class variable

 
 
jcteague@gmail.com
Guest
Posts: n/a
 
      10-30-2005
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

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-30-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-30-2005
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[i]; }
private:
set<int, Comp> selectedElements;
vector<int> x;
};

inline bool Comp:perator()(int a, int b) const
{
return ptr->getX(a) < ptr->getX(b);
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
can I declare a class variable inside that class method? John Black C++ 2 06-15-2004 03:24 PM



Advertisments