473,466 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

set comparison method for class variable

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 '05 #1
2 3688
jc******@gmail.com 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
Oct 30 '05 #2
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::operator()(int a, int b) const
{
return ptr->getX(a) < ptr->getX(b);
}
Oct 30 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
4
by: Peter Kirk | last post by:
Hi I am looking at some code which in many places performs string comparison using == instead of Equals. Am I right in assuming that this will in fact work "as expected" when it is strings...
21
by: tshad | last post by:
I tried to do this: public static void GetValueFromDbObject(object dbObjectValue, ref decimal destination) { destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue :...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
0
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
11
by: Steven D'Aprano | last post by:
I'm writing a class that implements rich comparisons, and I find myself writing a lot of very similar code. If the calculation is short and simple, I do something like this: class Parrot: def...
3
by: =?Utf-8?B?R0I=?= | last post by:
I have created a small program that illustrates the problem. I would know how to address the fields that I want to sort on in the greaterThan comparison. Anybody who knows?? using System; ...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.