Hello, after I have friendet my class to "<" operator it dosn't work after i use sort over an vector of class instances it does nothing:
class + main :

class RAngle{
 public:
        int x,y,l;
        RAngle(){
        }

    RAngle(int i,int j,int k){
        x=i,y=j,l=k;
    }

    bool operator<(const RAngle& rhs)const
    {
        if(l < rhs.l){
            return true;
        }
    return 0;
    }


    friend ostream& operator << (ostream& out, const RAngle& ra){
        out << ra.x << " " << ra.y << " " << ra.l <<endl;
        return out;
    }

    friend istream& operator >>( istream& is, RAngle &ra)
    {
        is >> ra.x;
        is >> ra.y;
        is >> ra.l;

        return is ;
    }

};



//main:
    void descrSort(vector <RAngle> &l){
        sort(l.begin(),l.end());
        reverse(l.begin(),l.end());

        for(unsigned i=0; i<l.size();i++){ //////at print the output is the same as the input ////
            cout<<l[i]<<endl;
        }
    }

    void readData(vector <RAngle> &l){
        RAngle r;
        ifstream f_in;

        f_in.open("ex.in",ios::in);

        for(int i=0;i<10;++i){
            f_in >> r;
            cout << r;
            l.push_back(r);

        }
    }

    int main(){
        vector <RAngle> a;
        vector <RAngle> b;
        readData(a);
        descrSort(a);

    return 0;
    }

Sorry for the long post, but I'm really without ideas why doesn't it work.

It seems to work on my end (noting that I removed readData() and built the vector manually). Can you post some sample data that exhibits the problem?

Your program works, just doesn't work correctly. The sort is wrong, you need to add the third parameter to std::sort and write a comparison function that returns bool value. The default third parameter is useful for only POD (plain old data) types and std::string.

    bool compare(RAngle& a, RAngle& b)
    {
        return a.x < b.x;
    }

sort(l.begin(),l.end(), compare);

EDIT: This is what the print parts show me... considering the input objects sort by the 3rd value... it is not sorted at all.
input:
* 1 7 31
* 2 2 2
* 3 3 3
* 4 5 1
* 10 5 1
* 1 1 9
* 10 3 10
* 4 5 7
* 5 4 15
* 2 3 25
* 1 7 31

after sort:Emphasized Text Here
* 2 2 2
* 3 3 3
* 4 5 1
* 10 5 1
* 1 1 9
* 10 3 10
* 4 5 7
* 5 4 15
* 2 3 25

Your program works, just doesn't work correctly. The sort is wrong, you need to add the third parameter to std::sort and write a comparison function that returns bool value.

That's unnecessary if the class overloads operator<(), which in this case it does.

EDIT: This is what the print parts show me... considering the input objects sort by the 3rd value... it is not sorted at all.

Once again, I'm getting correct results. What compiler and operating system are you using? Here's the compilable code I'm using to test on Visual Studio 2010, Windows 7 64-bit:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>

#include <cstdlib>

using namespace std;

class RAngle{
public:
    int x,y,l;
    RAngle(){
    }

    RAngle(int i,int j,int k){
        x=i,y=j,l=k;
    }

    bool operator<(const RAngle& rhs)const
    {
        if(l < rhs.l){
            return true;
        }
        return 0;
    }

    friend ostream& operator << (ostream& out, const RAngle& ra){
        out << ra.x << " " << ra.y << " " << ra.l;
        return out;
    }

    friend istream& operator >>( istream& is, RAngle &ra)
    {
        is >> ra.x;
        is >> ra.y;
        is >> ra.l;

        return is ;
    }
};

void descrSort(vector <RAngle> &l){
    for(unsigned i=0; i<l.size();i++){
        cout<<l[i]<<endl;
    }

    cout << "==================" << endl;

    sort(l.begin(),l.end());
    reverse(l.begin(),l.end());

    for(unsigned i=0; i<l.size();i++){
        cout<<l[i]<<endl;
    }
}

void readData(vector <RAngle> &l){
    RAngle r;
    ifstream f_in;

    f_in.open("test.txt",ios::in);

    for(int i=0;i<10;++i){
        f_in >> r;
        l.push_back(r);
    }
}

int main(){
    vector <RAngle> a;

    readData(a);
    descrSort(a);
}

And here are the contents of the file "test.txt":

1 7 31
2 2 2
3 3 3
4 5 1
10 5 1
1 1 9
10 3 10
4 5 7
5 4 15
2 3 25
1 7 31

Output is as follows:

1 7 31
2 2 2
3 3 3
4 5 1
10 5 1
1 1 9
10 3 10
4 5 7
5 4 15
2 3 25
==================
1 7 31
2 3 25
5 4 15
10 3 10
1 1 9
4 5 7
3 3 3
2 2 2
10 5 1
4 5 1
Press any key to continue . . .

I use win 7, 64-bit, mingw with Eclipse.

I use win 7, 64-bit, mingw with Eclipse.

I don't know what to tell you except hope for someone who can reproduce the issue. I get the same correct results with MinGW (g++ version 4.6.0). Unless you've got a buggy version of g++ (highly doubtful), something else is going on. But it's extremely difficult to troubleshoot problems that cannot be reproduced. :(

That's unnecessary if the class overloads operator<(), which in this case it does.

Apparently it is necessary because that's what made it work for me (using VC++ 2012 RC)

I've looked over your code and as deceptikon said, it works fine.
Tested on win 7 ultimate on x64 with MinGW and gcc 4.6.1, thus the error lies somewhere else.
I would suggest thou changing the operator< function to this:

bool operator<(const RAngle& rhs)const {
        return (l<rhs.l);
    }

This will take the implicit operator< from C++, rather than making the comparison yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.