J
jeremit0
I'm trying to sort a vector<complex<double> > and can't figure it
out. I recognize the problem is that there isn't a default operator<
for complex data types. I have written my own operator and can use
it, but std::sort doesn't seem to find it. I have copied a very
simple example below. Everything compiles just fine when the line
with std::sort function is commented out, but with that line included
a whole slew of errors are given like:
/usr/include/c++/4.0.0/bits/stl_algo.h:2996: error: no match for
‘operator<’ in ‘* __first2 < * __first1’
Can someone help me?
Thanks,
Jeremy
#include<iostream>
#include<algorithm>
#include<vector>
#include<complex>
using std::vector;
using std::cout;
using std::endl;
using std::complex;
inline bool operator< (complex<double>& x, complex<double>& y){
return x.real() < y.real();
}
int main(){
vector<complex<double> > values(3);
values[0] = 9.0; values[1] = 8.0; values[2] = 7.0;
vector<pair> Sets(values.size());
if( values[0] < values[1] ) cout << "first" << endl;
else cout << "second" << endl;
std::stable_sort(values.begin(), values.end());
return 0;
}
out. I recognize the problem is that there isn't a default operator<
for complex data types. I have written my own operator and can use
it, but std::sort doesn't seem to find it. I have copied a very
simple example below. Everything compiles just fine when the line
with std::sort function is commented out, but with that line included
a whole slew of errors are given like:
/usr/include/c++/4.0.0/bits/stl_algo.h:2996: error: no match for
‘operator<’ in ‘* __first2 < * __first1’
Can someone help me?
Thanks,
Jeremy
#include<iostream>
#include<algorithm>
#include<vector>
#include<complex>
using std::vector;
using std::cout;
using std::endl;
using std::complex;
inline bool operator< (complex<double>& x, complex<double>& y){
return x.real() < y.real();
}
int main(){
vector<complex<double> > values(3);
values[0] = 9.0; values[1] = 8.0; values[2] = 7.0;
vector<pair> Sets(values.size());
if( values[0] < values[1] ) cout << "first" << endl;
else cout << "second" << endl;
std::stable_sort(values.begin(), values.end());
return 0;
}