473,386 Members | 1,785 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,386 software developers and data experts.

Sorting by class value

Suppose I have:
#include <vector>
using std::vector;

class C
{
public:
int a,b;
};

int main()
{

vector<C> vecC;
//Populate vecC

how do i now sort according to a or b?? do I pass in a function pointer
to sort(.....)??
}
Regards

Mike
Jul 22 '05 #1
4 1221
"Michael" <sl***********@hotmail.com> writes:
[snip]
how do i now sort according to a or b?? do I pass in a function pointer
to sort(.....)??


You may use either a pointer to a function or a functor:

#include <vector>
#include <algorithm>
using std::vector;
using std::sort;

class C
{
public:
int a,b;
};

bool comp(C const& lhs, C const& rhs){return lhs.a < rhs.a;}

struct compare
{
bool operator()(C const& lhs, C const& rhs){return lhs.a < rhs.a;}
};
int main()
{
vector<C> vecC;
//Use pointer to function.
sort(vecC.begin(),vecC.end(),comp);
//Use functor.
sort(vecC.begin(),vecC.end(),compare());
}
Jul 22 '05 #2
Michael wrote:
Suppose I have:
#include <vector>
using std::vector;

class C
{
public:
int a,b;
};

int main()
{

vector<C> vecC;
//Populate vecC

how do i now sort according to a or b?? do I pass in a function pointer
to sort(.....)??
}
Regards

Mike


Create an ordering functor:
class a_less_b
{
public:
bool operator() (const C& c1, const C& c2)
{return c1.a < c2.b;}
};

Use the function when sorting the vector:
std::sort(vecC.begin(), vecC.end(), a_less_b());
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3
Michael wrote:
Suppose I have:
#include <vector>
using std::vector;

class C
{
public:
int a,b;
};

int main()
{

vector<C> vecC;
//Populate vecC

how do i now sort according to a or b?? do I pass in a function pointer
to sort(.....)??
}


You could overload the < operator for C
class C {
public:
int a,b;
bool operator < ( C c )
{ return a < c.a; }
}
Jul 22 '05 #4
"llewelly" <ll*********@xmission.dot.com> wrote in message
struct compare
{
bool operator()(C const& lhs, C const& rhs){return lhs.a < rhs.a;}
};


Typically it should be a const function.

bool operator()(C const& lhs, C const& rhs) const {return lhs.a < rhs.a;}
Jul 22 '05 #5

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

Similar topics

9
by: p0wer | last post by:
Let's suppose I have this sample document: <root> <entry id="1" <date>2003-08-03</date> <param_1>5</param_1> <param_2>10</param_2> </entry> <entry id="2"> ...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
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...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.