stl::sort() is changing the attributes of the objects it is sorting
<p>I've been coding a solution for the Graph Coloring Problem using Brown's algorithm. The algorithm is working just fine but, to add a little efficiency I'm trying to sort the nodes by their degree. To do so I'm using <code>stl::sort()</code>. Nonetheless, after sort has ordered the elements, the adyacency list of each element has been modified. I've got 4 files:</p>

<p>nodo.h:</p>

<pre><code>class Nodo{
private:
    int id;
    int color;
    vector&lt;Nodo*&gt; adyacentes;
public:
    int grado;
    vector&lt;Nodo*&gt; getAdyacentes();
    int getId();
    int getColor();
}
</code></pre>

<p>nodo.cpp -> Includes the basic implementation of the getters.</p>

<p>grafo.h:</p>

<pre><code>class Grafo{
private:
    Nodo *nodos;
    int tam;
public:
    void colorearBrown();
    void imprimir();
}
</code></pre>

<p>grafo.cpp:</p>

<pre><code>void Grafo::imprimir(){
    cout &lt;&lt; setw(6) &lt;&lt; "Numero" &lt;&lt; setw(5) &lt;&lt; " Color"  &lt;&lt; " Nodos adyacentes" &lt;&lt; endl;
    for(int i = 0; i &lt; tam; ++ i){
        cout &lt;&lt; setw(6) &lt;&lt; nodos[i].getId() &lt;&lt; setw(5) &lt;&lt; nodos[i].getColor() &lt;&lt; "  ";
        vector&lt;Nodo*&gt; ady = nodos[i].getAdyacentes();
        for(vector&lt;Nodo*&gt;::iterator it = ady.begin(); it != ady.end(); ++ it){
            if (it != ady.begin()){
                cout &lt;&lt; ",";
            }
            cout &lt;&lt; (*it)-&gt;getId();
        }
        cout &lt;&lt; endl;
    }
}

bool operator&lt;(const Nodo&amp; a, const Nodo&amp; b){
    return (a.grado &gt;= b.grado);
}

void Grafo::colorearBrown(){
    imprimir();
    sort(&amp;nodos[0], &amp;nodos[tam]);
    cout &lt;&lt; endl &lt;&lt; endl;
    imprimir();
}
</code></pre>

<p>And that's it. Assuming I've a correctly loaded graph in a 'Grafo' object, I run the method <code>colorearBrown()</code> and get the following output (part):</p>



<pre class="lang-none prettyprint-override"><code>.
.
.
    16  -1  1,22,36,43,47,48
    17  -1  10,13,20,22,44,47
    18  -1  27,32,48
    19  -1  4,32,36
    20  -1  6,8,10,12,17,33,36,38,43,45,48
    21  -1  4,6,45
    22  -1  6,16,17,26,30,31
    23  -1  3,8,10,14,24,26,32,36
.
.
.

Numero Color Nodos adyacentes
    20  -1  4,42,22,32,10,3,34,50,18,19,25
     7  -1  20,32,6,13,3,50,2,18,31,19
    36  -1  7,14,16,44,12,38,30,1,50,37
    14  -1  22,30,49,24,2,11,40,5
.
.
.
</code></pre>

<p>See how the list of adyacent nodes changes on node 20(here), but this is repeated for all nodes.</p>

<p>The program perfectly compiles and, if not sorted, the nodes stay the same way throughout the hole execution.</p>

<p>Any ideas on why the sorting algorithm is messing with my structure would be helpful.</p>
