___ erkimt ___ List operations (static) c ++ ______ qstntxt ___
Good afternoon guys, I'm having trouble understanding operations on static lists, I know how to create and display, but how do I remove and search within the list, sort algorithms I've already understood some, can help me, please? I'm kind of desperate.
pessoa* alguem[maximo];
for (int i = 0; i < maximo; i++) {
alguem[i] = new pessoa;
//...
}
The most practical suggestion to implement is what `` Nex '' and `` bownown '' have spoken: use the already ready C ++ frameworks ( vector , list , etc.). They already come with their own optimized search, insertion and removal methods.
If you really need to use array, you can try the following approach:
Use pointer vector
I find it easier to find the elements already removed / end of list.
void busca(pessoa* alguem[], int idade) {
for (int i = 0; i < maximo; i++) {
if (alguem[i] != NULL && alguem[i]->idade == idade) {
//Achou e pára a busca
}
}
//Não achou
}
Choose a field to search
To do a search, you have to assign some field to find the element (index, name, etc.).
//Removendo o elemento fazendo com que o índice aponte para nulo
void remove(pessoa* alguem[], int idade) {
for (int i = 0; i < maximo; i++) {
if (alguem[i] != NULL && alguem[i]->idade == idade) {
alguem[i] = NULL;
return;
}
}
}
Choose a field to search and remove
Here you can follow the same search logic to find the element to be removed. Then you can either point the vector index to null (simpler), resize the vector, or create a new vector with size n-1 , where n is the size Vector illustration. Could anything of the sort:
pessoa* alguem[maximo];
for (int i = 0; i < maximo; i++) {
alguem[i] = new pessoa;
//...
}