473,320 Members | 1,612 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.

how to use stl sort in this case

i wanna sort the elements stored in a vector, but my sort function
needs be a little different from the standard one, i wanna the sort
function accept 3 parameters.

stl::sort(myvec.begin(), myvec.end(), mysortfunc);

bool mysortfunc(elem1, elem2, anoter parameter)
{
//is it possible ??
}

Nov 20 '06 #1
2 1700
i wanna sort the elements stored in a vector, but my sort function
needs be a little different from the standard one, i wanna the sort
function accept 3 parameters.

stl::sort(myvec.begin(), myvec.end(), mysortfunc);

bool mysortfunc(elem1, elem2, anoter parameter)
{
//is it possible ??
}
functor meets your requirement:
#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;

class comparer {// comparer is a funtor
public:
comparer(int c): c(c) {} // c is the third parameter.
template <class T>
bool operator ()(const T& a, const T& b) const { // you can use the
third parameter here
cout << "comparing " << a << " and " << b << " using parameter: " <<
c << endl;
return a < b;
}
private:
int c; // the variable hold the third parameter.
};

int main(int argc, char* argv[])
{
vector<intv;
for(int i=1; i<10; i++)
{
v.push_back(i * i % 13);
}
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
sort(v.begin(), v.end(), comparer(5));
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
return 0;
}

Nov 20 '06 #2

thinktwice wrote:
i wanna sort the elements stored in a vector, but my sort function
needs be a little different from the standard one, i wanna the sort
function accept 3 parameters.

stl::sort(myvec.begin(), myvec.end(), mysortfunc);

bool mysortfunc(elem1, elem2, anoter parameter)
{
//is it possible ??
}
#include <boost/bind.hpp>

stl::sort(myvec.begin(), myvec.end(), boost::bind(&mysortfunc. _1, _2,
another_parameter));

Nov 20 '06 #3

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

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
1
by: Aravind | last post by:
Hi folks. My form, frmHistory, has a combo box, cboName, that filters frmHistory by member's name (using a macro). frmHistory also has a few labels that I use for sorting the form (using VBA...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
0
by: thomasp | last post by:
I have a datagridview that allows the user to sort the column by clicking on the header of the column the user wishes to sort. I did not code this, it had the ability when the datagrid was...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: usaccess | last post by:
Hi, I have a form/subform. There is a priority field and then a case status field. I want to display records so that it sorts by case status as "actives" then "on holds" then "completes" and...
3
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.