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

How to sort vector<complex<double> >

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<pairSets(values.size());

if( values[0] < values[1] ) cout << "first" << endl;
else cout << "second" << endl;

std::stable_sort(values.begin(), values.end());
return 0;
}

Jun 27 '08 #1
5 6021
jeremit0 wrote:
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<pairSets(values.size());

if( values[0] < values[1] ) cout << "first" << endl;
else cout << "second" << endl;

std::stable_sort(values.begin(), values.end());
return 0;
}
I think you'll need to place your operator in the std namespace. The
better option would be to pass a comparator to std::sort instead of
using the default (std::less).

bool complex_less_pred(complex<doubleconst& x, complex<doubleconst& y)
{
return x.real() < y.real();
}

....
std::stable_sort(begin, end, &complex_less_pred);
Jun 27 '08 #2
On Jun 25, 11:21*am, Noah Roberts <u...@example.netwrote:
jeremit0 wrote:
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<pairSets(values.size());
* * if( values[0] < values[1] ) cout << "first" << endl;
* * else cout << "second" << endl;
* * std::stable_sort(values.begin(), values.end());
* * return 0;
}

I think you'll need to place your operator in the std namespace. *The
better option would be to pass a comparator to std::sort instead of
using the default (std::less).

bool complex_less_pred(complex<doubleconst& x, complex<doubleconst& y)
{
* *return x.real() < y.real();

}

...
std::stable_sort(begin, end, &complex_less_pred);
Thank you, both techniques worked well. I'm curious why the operator
needs to be in the std namespace. Is that a requirement for the sort
algorithm?

Thanks again,
Jeremy
Jun 27 '08 #3
On Jun 25, 4:55 pm, jeremit0 <jerem...@gmail.comwrote:
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.
Normal. Sort is a template function, which uses another
template, std::less, in which the < operator is in a dependent
expression. Which means that if it isn't found in the context
where the template is defined, only ADL is used, and so only
operators in the associated namespaces and classes (in this case
std::) are found.
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?
The basic problem is that you're trying to do something that
you're not allowed to do: define an operator on a standard type
(where it doesn't make sense). The real solution is to define
some sort of ordering function (or functional object) yourself,
and pass that to sort as the third argument.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4
jeremit0 wrote:
On Jun 25, 11:21*am, Noah Roberts <u...@example.netwrote:
>jeremit0 wrote:
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.
[snip]
>I think you'll need to place your operator in the std namespace. *The
better option would be to pass a comparator to std::sort instead of
using the default (std::less).

bool complex_less_pred(complex<doubleconst& x, complex<doubleconst&
y)
{
return x.real() < y.real();

}

...
std::stable_sort(begin, end, &complex_less_pred);

Thank you, both techniques worked well. I'm curious why the operator
needs to be in the std namespace. Is that a requirement for the sort
algorithm?
No.

I think, ut's a lookup thing. The template std::complex is in namespace std,
and so are std::less and std::sort. Thus, the global namespace will not be
searched; and operator< for complex numbers will not be found if it isn't
in namespace std, too. (Or something like that.)
Best

Kai-Uwe Bux
Jun 27 '08 #5
jeremit0 wrote:
Thank you, both techniques worked well. I'm curious why the operator
needs to be in the std namespace. Is that a requirement for the sort
algorithm?
I actually don't know. I probably should. It's got something to do
with scope resolution, which can be weird wrt operators. Any operator
you want to overload for an object in std:: seems to also need to be in
that namespace if you ever use the overload through another function or
object in std::.
Jun 27 '08 #6

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

Similar topics

1
by: Chung Leong | last post by:
In an attempt to get another discussion going about security in PHP, I've put together this little exercise, the purpose of which is to illustrate how bedevilingly tricky writing secure code can be...
31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
10
by: bluekite2000 | last post by:
and why doesnt the standard vector have such conversion available?
14
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
5
by: Kenneth | last post by:
<list> seems to be a powerful structure to store the related nodes in memory for fast operations, but the examples I found are all related to primitive type storage. I'm doing a project on C++...
2
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator...
3
by: Klaas Vantournhout | last post by:
Hi, I am using CLAPACK to do lots of matrix operations, but this is done on complex matrices. There I also need to work with normal complex operators, I use also the standard complex library. ...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.