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(itemASorte d.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!! 2 2079
"Rachel Forder" <mi*********@ya hoo.com> wrote in message
news:19******** *************** ***@posting.goo gle.com...
.... 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(itemASorte d.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 OrderClassAItem A
{
vector<A*>* data_;
public:
OrderClassAItem A(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.beg in(), itemASorted.end ()
, OrderClassAItem A(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++
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 OrderClassAItem A { vector<A*>* data_; public: OrderClassAItem A(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.beg in(), itemASorted.end () , OrderClassAItem A(vecData) );
One such class can be written for each sort order...
I hope this helps, Ivan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 difference in minutes,
divide this result by a predefined size of interval, and make a loop
that runs this many times. For example, with an interval size of 15
minutes, it will return 6 blocks... I have this all working fine, but am
getting stuck on...
|
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 containing norwegian special
characters , and .
>>> liste =
|
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
|
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:
li=;
|
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. It is
almost like it is trying to implement it's own COM interfaces...
below is the header, and a link to the dll+code:
Zip file with header, example, and DLL:...
| |
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 * 31.9 = 95.7
@ the second time, 3 * 31.9 = 95.69999999999999
this is the list number which calculation is NOT correct
6 * 31.9 = 191.39999999999998
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 expandable row, the
hidden row is made visible with css. The problem is when i sort the
rows, the hidden rows get sorted as well which i don't want and want
to be moved (while sorting) relative to their parent rows. The
following is my complete html code...
|
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 list athe result is that the standard (QWERTY) keyboard
appears.
I found that many ( all?) ISV's who make IM's have customers reporting this
problem. My research on the internet brought me to the conclusion it is some
|
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 huge problems with slow connect times and heavy resource locking in high concurrency / high transaction volume environments. I have an OLTP with 30-90 transactions per second activity, and start of Health Monitor every 2 hours was crashing our...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |