How can I use sort with a pointer vector?

4

I'm trying to use sort with a vector type vector<node*> where node is a variable that I created myself. However I am not succeeding, it does not let me put that function inside the sort method. This is my code:

int compOrganizar(node* nodo1, node* nodo2){

    return (nodo1->getF() < nodo2->getF());
}

void expandir_nodo(node nodo){

    if (isGoal(nodo)){

        haveSol = true;
    }

    else{
        generateIzquierda(nodo);
        generateDerecha(nodo);

        sort(abiertos.begin(),abiertos.end(),compOrganizar);
    }

Would anyone know how to tell me what I have wrong? Thank you in advance to everyone.

    
asked by Alfonso Rodríguez 15.12.2017 в 12:53
source

2 answers

5

You can use the std::sort <algorithm> :

std::vector<node *> nodos;

// rellenar vector...

// ordenar el vector...
std::sort(nodos.begin(), nodos.end());

But since the type stored in the std::vector is a pointer it will sort you by the value of the pointer, which does not make any sense.

Luckily, std::sort accepts an additional parameter that consists of a function to sort your type, assuming you want to sort the nodes by node::getF you could do:

std::vector<node *> nodos;

// rellenar vector...

// ordenar el vector...
std::sort(nodos.begin(), nodos.end(), [](const node *&a, const node *&b)
{
    return a->getF() < b->getF();
});

The third parameter that I am passing to std::sort is a lambda expression .

    
answered by 15.12.2017 / 12:54
source
5

It is not working because the function that expects sort must return a Boolean:

   bool compOrganizar(node* nodo1, node* nodo2){
// ^^^^ 

    return (nodo1->getF() < nodo2->getF());
}

We must not lose sight of the fact that C ++ is based on a strong typing ... it is more fussy than the compiler of C

    
answered by 15.12.2017 в 13:01
___ ___ erkimt How hidden an entire column in a table with javascript / jquery? ______ qstntxt ___

I want to show / hide a column of a table using javascript / jquery .

Pressing a button shows or hides the entire column. The name of the column is on TH identified by an id I have achieved this by adding a class to each TD but I do not like this solution, I would like to achieve it without adding so much class attribute.

%pre% %pre%
    
______ azszpr135702 ___

Without so much code can be fixed by putting 1 button, instead of 2.

Unless it's strictly necessary, I think it's better if you only have one button, that you make %code% that commutes the visibility of the column.

To do this, we select the header with %code% and hide it or show it with %code% .

To hide / show the rows, we can make a small filter like this:

%pre%

which means: Take the %code% that are your father's third child. The parent is the row %code% and the third child is the %code% in the Country column.

and to them you apply the %code% as well.

With this you save putting the %code% to each element you want to hide.

%pre% %pre%
    
______ azszpr135706 ___

To hide with %code% , without Jquery, it occurs to me that since you already have a class that identifies you, you can use them to add a class %code% , for example.

%pre% %pre% %pre%

If you also take into account the modification of %code% , you could delete the classes of each %code% and only add a %code% to the table as well as remove a button and have only %code% that works as a %code% , but all only with only %code%

%pre% %pre% %pre%
    
______ azszpr135708 ___

You can also do the following from CSS:

%pre%

Or have a class hide in CSS and by means of javascript, add the class:

%pre%     
___
String Separator in Java