<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[List sort wird trotz operator nicht soertiert]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich versuche eine liste zu sortieren doch die Liste wird nicht sortiert. Ich habe im operator eine Ausgabe der Werte gemacht und die Werte werden als 0 ausgegeben. Ich verstehe das nicht. Hier mal das Beispielcode:</p>
<pre><code class="language-cpp">class Data{
    int age;
    string name;
};

typedef std::list&lt;Data&gt; Customers;

bool operator&lt;(Data &amp;x, Data &amp;y){
    cout &lt;&lt; x.age &lt;&lt; &quot; &quot; &lt;&lt; y.age &lt;&lt; endl; // wird immer 0 0 ausgegeben
    return x.age &lt; y.age;
}

class CustomerMGNT{

    Customers customers;

public:

    void add(Data customer){
        this-&gt;customers.push_back(customer);
    }
    void printsorted(){
        list&lt;Data&gt;::iterator it;
        this-&gt;customers.sort();
        for (it=this-&gt;customers.begin();it != this-&gt;customers.end(); ++it){
	    cout &lt;&lt; (*it).name &lt;&lt; &quot; &quot;;
	    cout &lt;&lt; (*it).age &lt;&lt; endl;
        }
    }
};

int main(){
    CustomerMGNT cu;
    Data a,b.c;

    a.age = 53;
    a.name = &quot;Test01&quot;;

    b.age = 23;
    b.name = &quot;Test02&quot;;

    c.age = 33;
    c.name = &quot;Test03&quot;;

    cu.add(a);
    cu.add(b);
    cu.add(c);
    cu.printsorted();
    return 1;
}
</code></pre>
<p>output:<br />
0 0<br />
0 0<br />
0 0<br />
Test01 53<br />
Test02 23<br />
Test03 33</p>
<p>Ich verstehe das nicht. Warum bekommt der operator 0 als age?? Kann mir jemand weirwe helfen???</p>
<p>Danke + Gruss,<br />
Haksi</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/246510/list-sort-wird-trotz-operator-nicht-soertiert</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 01:51:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/246510.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 29 Jul 2009 11:39:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to List sort wird trotz operator nicht soertiert on Wed, 29 Jul 2009 11:39:57 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich versuche eine liste zu sortieren doch die Liste wird nicht sortiert. Ich habe im operator eine Ausgabe der Werte gemacht und die Werte werden als 0 ausgegeben. Ich verstehe das nicht. Hier mal das Beispielcode:</p>
<pre><code class="language-cpp">class Data{
    int age;
    string name;
};

typedef std::list&lt;Data&gt; Customers;

bool operator&lt;(Data &amp;x, Data &amp;y){
    cout &lt;&lt; x.age &lt;&lt; &quot; &quot; &lt;&lt; y.age &lt;&lt; endl; // wird immer 0 0 ausgegeben
    return x.age &lt; y.age;
}

class CustomerMGNT{

    Customers customers;

public:

    void add(Data customer){
        this-&gt;customers.push_back(customer);
    }
    void printsorted(){
        list&lt;Data&gt;::iterator it;
        this-&gt;customers.sort();
        for (it=this-&gt;customers.begin();it != this-&gt;customers.end(); ++it){
	    cout &lt;&lt; (*it).name &lt;&lt; &quot; &quot;;
	    cout &lt;&lt; (*it).age &lt;&lt; endl;
        }
    }
};

int main(){
    CustomerMGNT cu;
    Data a,b.c;

    a.age = 53;
    a.name = &quot;Test01&quot;;

    b.age = 23;
    b.name = &quot;Test02&quot;;

    c.age = 33;
    c.name = &quot;Test03&quot;;

    cu.add(a);
    cu.add(b);
    cu.add(c);
    cu.printsorted();
    return 1;
}
</code></pre>
<p>output:<br />
0 0<br />
0 0<br />
0 0<br />
Test01 53<br />
Test02 23<br />
Test03 33</p>
<p>Ich verstehe das nicht. Warum bekommt der operator 0 als age?? Kann mir jemand weirwe helfen???</p>
<p>Danke + Gruss,<br />
Haksi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1751505</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1751505</guid><dc:creator><![CDATA[haksi74]]></dc:creator><pubDate>Wed, 29 Jul 2009 11:39:57 GMT</pubDate></item><item><title><![CDATA[Reply to List sort wird trotz operator nicht soertiert on Wed, 29 Jul 2009 12:17:55 GMT]]></title><description><![CDATA[<p>Dieser Code kompiliert nicht. Wenn man ihn soweit korrigiert, dass er kompiliert, ist dein beschriebener Fehler nicht reproduzierbar.</p>
<p>Welchen Compiler benutzt du?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1751551</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1751551</guid><dc:creator><![CDATA[Registrierter Troll]]></dc:creator><pubDate>Wed, 29 Jul 2009 12:17:55 GMT</pubDate></item></channel></rss>