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

why std::sort take no template parameter to swap?

Hi there! I wonder why there is no such a overload version of sort
function in STL:

template<class RandomAccessIterator, class Pr, class IterSwap>
void sort(
RandomAccessIterator _First,
RandomAccessIterator _Last,
BinaryPredicate _Comp,
IterSwap _Swap
);

It seems that std::sort works only with "simple" element, which has
default constructor and value semantics. So the problem rises when I
want to sort a collection of point coordinates, like:

// sort 2D points, note that I use double(*)[2] to hold points'
coordinates.
void SortPoint(double coords[][2], int n)
{
sort(coords, coords + n, PointLess()); // compile error in
std::swap, require l-value.
}

What I want is somthing like:

void SortPoint(double coords[][2], int n)
{
sort(coords, coords + n, PointLess(), PointSwap());
}

Wish somebody to share his(her) thought about this!

// compare points
class PointLess
{
public:
bool operator() (double* a, double* b)
{
if(abs(a[0] - b[0]) tolerance)
return a[0] < b[0];

return a[1] < b[1];
}
};

// swap points
class PointSwap
{
public:
void operator() (double* a, double* b)
{
swap(a[0], b[0]);
swap(a[1], b[1]);
}
};
Nov 27 '07 #1
1 1770
panzhiyong a écrit :
Hi there! I wonder why there is no such a overload version of sort
function in STL:

template<class RandomAccessIterator, class Pr, class IterSwap>
void sort(
RandomAccessIterator _First,
RandomAccessIterator _Last,
BinaryPredicate _Comp,
IterSwap _Swap
);

It seems that std::sort works only with "simple" element, which has
default constructor and value semantics. So the problem rises when I
want to sort a collection of point coordinates, like:

// sort 2D points, note that I use double(*)[2] to hold points'
coordinates.
void SortPoint(double coords[][2], int n)
{
sort(coords, coords + n, PointLess()); // compile error in
std::swap, require l-value.
}

What I want is somthing like:

void SortPoint(double coords[][2], int n)
{
sort(coords, coords + n, PointLess(), PointSwap());
}

Wish somebody to share his(her) thought about this!

// compare points
class PointLess
{
public:
bool operator() (double* a, double* b)
{
if(abs(a[0] - b[0]) tolerance)
return a[0] < b[0];

return a[1] < b[1];
}
};

// swap points
class PointSwap
{
public:
void operator() (double* a, double* b)
{
swap(a[0], b[0]);
swap(a[1], b[1]);
}
};

You could use an iterator_facade.Boost provides an implmentation but it
is included in TR1 so it is next to standard.

You basically wrap a double* pointer but:
* iterator increments are done by twos
* distance is given divided by two
* return type is a struct { double& x; double& y;}; to allow copy

Seems a bit of a pain but you only need to implement it once. I have
very little experience about it so I cannot evaluate if it is worth the
trouble.

Here is the example from Boost:
http://boost.org/libs/iterator/doc/i...torial-example
Michael
Nov 27 '07 #2

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

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
1
by: Ravi | last post by:
I came across code which contains: std::sort<unsigned char*>((newAnswer.begin()+1),newAnswer.end()); with newAnswer defined as std::vector<unsigned char>& newAnswer However upon...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
3
by: 77123036 | last post by:
why this programme cannot be built in vc6(there are two link error),but successful in gcc? ------------------ #include <iostream> #include <algorithm> template <class T> inline bool less(T...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
1
by: mscava | last post by:
How can I make this construction valid? It still gives me error about no matching function std::sort(...). I made a little search and wrong thing is probably the predicate... template <typename...
1
by: Markus Dehmann | last post by:
In the following code example, I define several Comparator classes which contain different compare functions to use with std::sort. I have a Sorter class that gets passed a Comparator and is...
10
by: ikarus | last post by:
Hello C++ Gurus! I'm comparing sorting algorithm for study goals. I've compared STL std::sort and hand-coded introsort on millions (tens of millions) of integers array sorting. It was tested...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.