Standart Template Library, Sorting a linked list of type struct

Can someone help me sorting a linked list of type struct using the standard template library. Documetation seems to be lacking, been searching for hours.

Supposedly this is the form to use but I got a list of data structures not ints how do you tell sort which element to sort by? I tried this form and it doesn't work for int's either. Some help or pointers in the right direction would be appreciated.
// file.h

class myclass {
public:
bool operator<(myclass& other);
private:
int x;
}

// file.cpp

bool myclass::operator<(myclass& arg) {
if (this->x < arg.x)
return(true);
else
return(false);
}


// main.cpp

listname.sort();

Comments

  • [blue]std::sort() works for arrays such as std::vector, not linked lists like std::list. [/blue]

    : Can someone help me sorting a linked list of type struct using the standard template library. Documetation seems to be lacking, been searching for hours.
    :
    : Supposedly this is the form to use but I got a list of data structures not ints how do you tell sort which element to sort by? I tried this form and it doesn't work for int's either. Some help or pointers in the right direction would be appreciated.
    : // file.h
    :
    : class myclass {
    : public:
    : bool operator<(myclass& other);
    : private:
    : int x;
    : }
    :
    : // file.cpp
    :
    : bool myclass::operator<(myclass& arg) {
    : if (this->x < arg.x)
    : return(true);
    : else
    : return(false);
    : }
    :
    :
    : // main.cpp
    :
    : listname.sort();
    :

  • So it can't be done with a data structure. That is what I need. Thanks stober for the help.


    : [blue]std::sort() works for arrays such as std::vector, not linked lists like std::list. [/blue]
    :
    : : Can someone help me sorting a linked list of type struct using the standard template library. Documetation seems to be lacking, been searching for hours.
    : :
    : : Supposedly this is the form to use but I got a list of data structures not ints how do you tell sort which element to sort by? I tried this form and it doesn't work for int's either. Some help or pointers in the right direction would be appreciated.
    : : // file.h
    : :
    : : class myclass {
    : : public:
    : : bool operator<(myclass& other);
    : : private:
    : : int x;
    : : }
    : :
    : : // file.cpp
    : :
    : : bool myclass::operator<(myclass& arg) {
    : : if (this->x < arg.x)
    : : return(true);
    : : else
    : : return(false);
    : : }
    : :
    : :
    : : // main.cpp
    : :
    : : listname.sort();
    : :
    :
    :

  • : So it can't be done with a data structure. That is what I need. Thanks stober for the help.
    :
    :
    [blue]that is not what I said. I said sort works on array, not linked lists. you can make an std::vector out of data structures -- afterall that is what the example code you posted was.[/blue]
  • : Can someone help me sorting a linked list of type struct using the standard template library. Documetation seems to be lacking, been searching for hours.
    :
    : Supposedly this is the form to use but I got a list of data structures not ints how do you tell sort which element to sort by? I tried this form and it doesn't work for int's either. Some help or pointers in the right direction would be appreciated.

    [blue]You can try making the less-than operator a global operator rather than strictly a member function. There is a difference in the way the member function would be called versus a global operator and maybe the call to the less-than operator within the [italic]sort[/italic] function call is doing it the other way.[/blue]

    : // file.h
    :
    : class myclass {
    : public:
    : [red]friend[/red] bool operator<([red]const[/red] myclass& lhs, [red]const myclass& rhs[/red]);
    : private:
    : int x;
    : }
    :
    : // file.cpp
    :
    : bool operator<([red]const[/red] myclass& lhs[red], const myclass& rhs[/red])
    : {
    : [red]return lhs.x < rhs.x;[/red]
    : }
    :
    :
    : // main.cpp
    :
    : listname.sort();
    :

  • Ok I've been playing around with it and see how it works a little better. The real question I need answered is how do I compare a struct within a class. Here is what I got so far.

    ://class.h
    :
    :class CList{
    : private:
    :
    : struct node_c{
    :
    : int x;
    : string y;
    : };
    :
    : public:
    : bool operator<(node_c& other);
    :};
    :
    ://class.cpp
    :bool CList::operator<(node_c& arg) {
    :
    :return (this->x < arg.x);
    :}

    Obviously this won't work becuase this is pointing to the class not the struct. Errors tell me x is not a member of CList.

    'x' : is not a member of 'CList'
    'x' : is not a member of 'CList::node_c'

  • : Ok I've been playing around with it and see how it works a little better. The real question I need answered is how do I compare a struct within a class. Here is what I got so far.
    :
    : ://class.h
    : :
    : :class CList{
    : : private:
    : :
    : : struct node_c{
    : :
    : : int x;
    : : string y;
    : : };
    : :
    : : public:
    : : bool operator<(node_c& other);
    : :};
    : :
    : ://class.cpp
    : :bool CList::operator<(node_c& arg) {
    : :
    : :return (this->x < arg.x);
    : :}
    :
    : Obviously this won't work becuase this is pointing to the class not the struct. Errors tell me x is not a member of CList.
    :
    : 'x' : is not a member of 'CList'
    : 'x' : is not a member of 'CList::node_c'
    :
    :

    You made an operator < to compare a Clist and a node_c, not two node_c.
    operator < should have been declared inside struct node_c. This way "this" will point to the a node_c class and these error messages will disappear.
    You can also declare operator < as follows:
    [code]bool CList::operator <(node_c& arg1, node_c& arg2){
    return arg1.x < arg2.x;
    }[/code]

    Homero C. de Almeida

    [italic]There's no dishonour in failure. For we aren't allowed to know wheter we'll achieve success or not. There is only one final shame, the cowardice of not trying.[/italic]

  • when I try CList::operator <(node_c& arg1, node_c& arg2){ compiler says: error C2804: binary 'operator <' has too many parameters. Where can I find syntax for defining operators? Why does it say that?


    : You can also declare operator < as follows:
    : [code]bool CList::operator <(node_c& arg1, node_c& arg2){
    : return arg1.x < arg2.x;
    : }[/code]

  • : Ok I've been playing around with it and see how it works a little better. The real question I need answered is how do I compare a struct within a class. Here is what I got so far.
    :
    : ://class.h
    : :
    : :class CList{
    : : private:
    : :
    : : struct node_c{
    : :
    : : int x;
    : : string y;
    : : };
    : :
    : : public:
    : : bool operator<(node_c& other);
    : :};
    : :
    : ://class.cpp
    : :bool CList::operator<(node_c& arg) {
    : :
    : :return (this->x < arg.x);
    : :}
    :
    : Obviously this won't work becuase this is pointing to the class not the struct. Errors tell me x is not a member of CList.
    :
    : 'x' : is not a member of 'CList'
    : 'x' : is not a member of 'CList::node_c'
    :
    :
    [purple]
    this might help, not sure whether or not it will work. right now, i dont have a compiler:[code]
    class CList{
    private:
    struct node_c{
    int x;
    string y;
    };
    public:
    bool operator<(const CList& other);
    };

    //class.cpp
    bool CList::operator<(const CList& other) {
    return (node_c::x < other.node_c::x);
    }
    [/code]
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • : when I try CList::operator <(node_c& arg1, node_c& arg2){ compiler says: error C2804: binary 'operator <' has too many parameters. Where can I find syntax for defining operators? Why does it say that?
    :
    :
    : : You can also declare operator < as follows:
    : : [code]bool CList::operator <(node_c& arg1, node_c& arg2){
    : : return arg1.x < arg2.x;
    : : }[/code]
    :
    :

    Sorry... Remove the operator < from the class signature, and then use
    [code]
    bool operator < (node_c& arg1, node_c& arg2) {
    return arg1.x < arg2.x;
    }
    [/code]

    Homero C. de Almeida

    [italic]There's no dishonour in failure. For we aren't allowed to know wheter we'll achieve success or not. There is only one final shame, the cowardice of not trying.[/italic]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories