1
Good afternoon guys, I’m having a hard time understanding the operations in Statist lists, I know create and display, but how do I remove and search within the list, the sorting algorithms I’ve already understood some, can help me please? I’m a little 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);
}
Is there any reason to use C in a code that’s supposed to be C++? The search is almost the same as the print, only it makes a comparison to see if the item is what you’re looking for. The removal is much more complicated, starting by searching for what you want to remove, unless you know its position. Then you will have to pull each item to the previous one since the item that was removed, and.
– Maniero
You could improve your question. What do you want to know exactly? Your code loops to a fixed size to add within an array of instances of the person struct, and then uses an equivalent loop to print what has been inserted. Now you want to search for a specific element of the array, and also remove a specific element?
– Rafael B.
@rafaelB. My problem is that I need to somehow delete an instance of the struct that’s already been put on the list, only I’m not able to develop the logic for such a.
– Tiago Silveira
Use Std::vector or Std::deck with both you can delete an element as long as you know its position, the position of all next elements will be automatically readjusted.
– Nex'