Comparator in sort

closed account (1vf9z8AR)
I am having a problem with using comparator in the sort function in c++.
The problem doesn't occur in codeblocks but on spoj online compiler, it gives an error.

Error-
prog.cpp:23:24: note: candidate expects 2 arguments, 3 provided
sort(p,p+n,time);

1
2
3
4
5
6
7
8
 bool time(pair<int,int>&a,pair<int,int>&b)
{
    return a.second<b.second;
}
int main()
{
  sort(p,p+n,time);
}


here p is an array of pairs, n is the size of the array.
std::sort can be called with either 2 or 3 arguments.
Is sort the std::sort function or sth. else?
Try renaming "time" to "by_time".

It's getting confused by time() in the C standard library. Even if I don't say "using namespace std" it still clashes in my compiler (is that standards conforming?).

The best fix is to use a different name, although you could also disambiguate your function from the library one by casting "time" to the type of your function in the call to sort.

1
2
3
    typedef std::pair<int,int> Pair;
    typedef bool(* CmpFunc )(Pair&,Pair&);
    std::sort(&p[0], &p[n], CmpFunc(time));

Last edited on
cpp.sh's compiler says:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <utility>
#include <algorithm>

bool time( const std::pair<int,int>& a, const std::pair<int,int>& b )
{
 return a.second < b.second;
}

bool foo( const std::pair<int,int>& a, const std::pair<int,int>& b )
{
 return a.second < b.second;
}

int main()
{
  constexpr size_t N {3};
  std::pair<int,int> p [N] {};
  std::sort( std::begin(p), std::end(p), time ); // error

  std::sort( std::begin(p), std::end(p), foo ); // ok

  std::sort( std::begin(p), std::end(p),
             [](auto lhs, auto rhs ){ return lhs.second < rhs.second; } ); // ok
}

 In function 'int main()':
18:47: error: no matching function for call to
 'sort(std::pair<int, int>*, std::pair<int, int>*, <unresolved overloaded function type>)'

18:47: note: candidates are:
A)
template<class _RAIter> void std::sort(_RAIter, _RAIter)
note: candidate expects 2 arguments, 3 provided

B)
template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)
note: couldn't deduce template parameter '_Compare'
<unresolved overloaded function type>

In other words, that GCC(?) does not even list all the overloaded time that it sees.
closed account (1vf9z8AR)
Thanks. It worked. I didn't know time was a pre-defined function.
The most likely place to see time used is in seeding of srand. One would include <ctime>.
C++ library has <chrono> and <random> to replace those.

The real question is, how did we include time to create the ambiguity? Lets test:
1
2
3
4
5
6
7
8
9
#include <algorithm>

bool time( double a, double b ) {
 return a < b;
}

int main() {
  auto x = time( 1, 2, 3 ); // error
}

 In function 'int main()':
8:26: error: no matching function for call to 'time(int, int, int)'
8:26: note: candidates are:

 In file included from /usr/include/pthread.h:24:0,
   from /usr/include/x86_64-linux-gnu/c++/4.9/bits/gthr-default.h:35,
   from /usr/include/x86_64-linux-gnu/c++/4.9/bits/gthr.h:148,
   from /usr/include/c++/4.9/ext/atomicity.h:35,
   from /usr/include/c++/4.9/bits/basic_string.h:39,
   from /usr/include/c++/4.9/string:52,
   from /usr/include/c++/4.9/random:40,
   from /usr/include/c++/4.9/bits/stl_algo.h:66,
   from /usr/include/c++/4.9/algorithm:62,
   from 1:
/usr/include/time.h:192:15: note: time_t time(time_t*)
  extern time_t time (time_t *__timer) __THROW;
/usr/include/time.h:192:15: note: candidate expects 1 argument, 3 provided

3:6: note: bool time(double, double)
3:6: note: candidate expects 2 arguments, 3 provided

Wow. Threaded strings need time.

Different compilers implement the Standard Library differently.
Overall, it used to be much worse than it is now.
Topic archived. No new replies allowed.