473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

problem related to the sort function provided by STL

Hi All,

I have a problem related to the sort function provided by STL.

class A{
A(string, string, int);
string itemA;
string itemB;
int itemC;
};

class B{
vector<A*> vecData;
vector<int> itemASorted;//keeps vecData indexes into sorted form
for itemA
vector<int> itemBSorted;//keeps vecData indexes into sorted form
for temA
vector<int> itemCSorted;//keeps vecData indexes into sorted form
for itemA
};

In my program, i fill my vecData with all the data avalible.
then i need to create the 3 sorted vectors where the indexes into
vecData is kept in sorted form according to the item.

i am having problem in defining compare function for the 3rd argument
of STL sort function

sort(itemASorted.begin(), itemASorted.end(), cmpFunc);

approach 1:
make cmpFunc as static member of class B,
but then it won't be able to access vecData variable which is
not static.
approach 2:
overload operator(),
but it can be done only for one item...whereas i need a vector
sorted for all the 3 items

any guesses how can i perform sorting of my items?

thanks for any help!!
Jul 19 '05 #1
2 2065
"Rachel Forder" <mi*********@yahoo.com> wrote in message
news:19**************************@posting.google.c om...
....
class A{
A(string, string, int);
string itemA;
string itemB;
int itemC;
};

class B{
vector<A*> vecData;
vector<int> itemASorted;//keeps vecData indexes into sorted form
for itemA
vector<int> itemBSorted;//keeps vecData indexes into sorted form
for temA
vector<int> itemCSorted;//keeps vecData indexes into sorted form
for itemA
};

In my program, i fill my vecData with all the data avalible.
then i need to create the 3 sorted vectors where the indexes into
vecData is kept in sorted form according to the item.

i am having problem in defining compare function for the 3rd argument
of STL sort function

sort(itemASorted.begin(), itemASorted.end(), cmpFunc); .... any guesses how can i perform sorting of my items?


You can use an auxillary class, one for each ordering criterion:

// class for sorting by itemA member of class A
class OrderClassAItemA
{
vector<A*>* data_;
public:
OrderClassAItemA(vector<A*>& data) : data_(&data) {}
void operator()(int a, int b)
{
return data_->at(a)->itemA < data_->at(b)->itemA;
}
};

Which is then used as follows from a method of class B:
sort( itemASorted.begin(), itemASorted.end()
, OrderClassAItemA(vecData) );

One such class can be written for each sort order...
I hope this helps,
Ivan
--
http://ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C++
Jul 19 '05 #2
thanks Ivan,
it worked.

Rachel
You can use an auxillary class, one for each ordering criterion:

// class for sorting by itemA member of class A
class OrderClassAItemA
{
vector<A*>* data_;
public:
OrderClassAItemA(vector<A*>& data) : data_(&data) {}
void operator()(int a, int b)
{
return data_->at(a)->itemA < data_->at(b)->itemA;
}
};

Which is then used as follows from a method of class B:
sort( itemASorted.begin(), itemASorted.end()
, OrderClassAItemA(vecData) );

One such class can be written for each sort order...
I hope this helps,
Ivan

Jul 19 '05 #3

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

Similar topics

2
by: Marcus | last post by:
I am having some problems with trying to perform calculations on time fields. Say I have a start time and an end time, 1:00:00 and 2:30:00 (on a 24 hour scale, not 12). I want to find the...
39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
4
by: Johan | last post by:
Hi, Why does my vector not sort. What I understand is you have to overload the < operator, but that does not work. see code below Thanks Johan
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
38
by: baong | last post by:
dear all i have use this line to time in many my web base appl. but now i found a weird problem the javascript line is the same but i use the calculation 2 time @ the first time, it is ok, 3 *...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
0
by: =?Utf-8?B?am8uZWw=?= | last post by:
Hello All, I am developing an Input Methop (IM) for PocketPC / Windows Mobile (PPC/WM). On some devices the IM will not start. The IM appears in the IM-List but when it is selected from the...
5
by: jbenner | last post by:
I have opened a PMR for this with IBM, and am not asking for advice from the DB2 DBA community. I am posting this as an FYI that DB2 Health Monitor, even at the latest version of DB2, still can cause...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.