List operations (static) c ++

1
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.

#include <iostream>
#include <cstring> //lib pra poder usar o strcpy
#define maximo 2
using namespace std;

struct pessoa {
  char nome [20];
  int idade;
  float altura;
  float peso;
};

void imprime (pessoa alguem[]){
  for (int i = 0; i < maximo; i++) {
    cout <<"Nome: "<< alguem[i].nome << " - ";
    cout <<"Idade: "<<alguem[i].idade << " - ";
    cout <<"Altura: " << alguem[i].altura << " - ";
    cout <<"Peso: "<< alguem[i].peso<< "\n";

  }
}
int main() {

pessoa alguem [maximo];

for (int i = 0; i < maximo; i++) {
  cout << "Insira o nome: " << '\n';
  cin >> alguem[i].nome;
  cout << "Insira a idade: " << '\n';
  cin >> alguem[i].idade;
  cout << "Insira a altura: " << '\n';
  cin >> alguem[i].altura;
  cout << "Insira o peso: " << '\n';
  cin >> alguem[i].peso;
}
  imprime (alguem);
}
    
asked by anonymous 12.03.2017 / 20:57

1 answer

0
___ 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;
    //...
}
    
______ azszpr190283 ___

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;
    //...
}
    
___
15.03.2017 / 15:50