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

sort() a vector: quick question



I want to sort a vector and am implementing a comparison
function, say my_comparison. I would like to send the comparison
function a third argument which changes from time to time.
I understand I can do this using a global variable for the
third variable. Any other suggestions on how to accomplish this?

Thanks,
--j

Jul 23 '05 #1
1 1440
"John" <we**********@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I want to sort a vector and am implementing a comparison
function, say my_comparison. I would like to send the comparison
function a third argument which changes from time to time.
I understand I can do this using a global variable for the
third variable. Any other suggestions on how to accomplish this?


Use a predicate class.

Here's a trivial example you should be able to adapt easily:

class MyCompare
{
bool inverse_; // use any data members you need
public:
MyCompare(bool inverse)
: inverse_(inverse) {}

bool operator( std::string const& a, std::string const& b )
{
if( inverse_ ) return b<a;
else return a<b;
}
};

void mySort( std::vector<std::string>& v, bool inverse )
{
std::sort( v.begin(), v.end(), MyCompare(inverse) );
}
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #2

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

Similar topics

3
by: Allan Bruce | last post by:
Hi there, I am reading through accelerated c++, and have just finshed chapter 3. My question is what sorting algorithm is used on vectors? e.g. if I had: std::vector<int> scores; // adding...
11
by: JerryJ | last post by:
I have to fetch values from a database and store them in a vector in my c++ application. What is faster? if i fetch the values sorted from the database with a 'order by' clause in the...
3
by: jack | last post by:
Hi there, Does STL provide some function to sort several variables together by one of them? Say I have 100 student's names, age and scores, and I want to sort them by score only. I know I...
12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
4
by: John | last post by:
I would like to sort a vector using a comparison function of my own, the problem is that the comparison function is dependent on the first element of the vector. I do not want to make this element...
8
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
11
by: Magcialking | last post by:
I wanna sort a pointer vector with the algorithm "sort" in STL like this: " int compare(vector<string*>::iterator a,vector<string*>::iterator b){ return (**a)<(**b); } ...
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...
4
by: Santosh Nayak | last post by:
Hi, Is it possible to sort the array of struct based on the data members. e.g. struct { int a ; float b ; char c ; } TEMP ;
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
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...
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,...

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.