How to sort vector<complex<double> >

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jeremit0

    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<iostre am>
    #include<algori thm>
    #include<vector >
    #include<comple x>

    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_sor t(values.begin( ), values.end());
    return 0;
    }

  • Noah Roberts

    #2
    Re: How to sort vector&lt;compl ex&lt;double&gt ; &gt;

    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<iostre am>
    #include<algori thm>
    #include<vector >
    #include<comple x>
    >
    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_sor t(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_pr ed(complex<doub leconst& x, complex<doublec onst& y)
    {
    return x.real() < y.real();
    }

    ....
    std::stable_sor t(begin, end, &complex_less_p red);

    Comment

    • jeremit0

      #3
      Re: How to sort vector&lt;compl ex&lt;double&gt ; &gt;

      On Jun 25, 11:21 am, Noah Roberts <u...@example.n etwrote:
      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<iostre am>
      #include<algori thm>
      #include<vector >
      #include<comple x>
      >
      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_sor t(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_pr ed(complex<doub leconst& x, complex<doublec onst& y)
      {
         return x.real() < y.real();
      >
      }
      >
      ...
      std::stable_sor t(begin, end, &complex_less_p red);
      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

      Comment

      • James Kanze

        #4
        Re: How to sort vector&lt;compl ex&lt;double&gt ; &gt;

        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:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Kai-Uwe Bux

          #5
          Re: How to sort vector&lt;compl ex&lt;double&gt ; &gt;

          jeremit0 wrote:
          On Jun 25, 11:21 am, Noah Roberts <u...@example.n etwrote:
          >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_pr ed(complex<doub leconst& x, complex<doublec onst&
          >y)
          >{
          >return x.real() < y.real();
          >>
          >}
          >>
          >...
          >std::stable_so rt(begin, end, &complex_less_p red);
          >
          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

          Comment

          • Noah Roberts

            #6
            Re: How to sort vector&lt;compl ex&lt;double&gt ; &gt;

            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::.

            Comment

            Working...