How to remove element from a list?

Asked

Viewed 45 times

1

I have a list of objects from the No class, and would like to remove an object from that list, I use the library list.

I know of the existence of pop_back() and pop_front(), but they delete exactly the first or last item from the list. In my case I will have a list of objects of this node class, and each object has its id, for example: No1(id=1),No2(id=2),No3(id=3),no4(id=4)...

I would like to remove id=2 for example, as I would?

Follow my class No.h:

#ifndef NO_H_INCLUDED
#define NO_H_INCLUDED

#include <list>
#include "Aresta.h"

using namespace std;

class No
{
    int id;
    list<Aresta> *adjNo;

    public:
    No(int id); // construtor
    int getId();
    list<Aresta>* getAdjNo();
    void inserirAdj(Aresta aresta);
};

#endif // NO_H_INCLUDED

No cpp.:

#include "No.h"
#include <list>

No::No(int id){
    this->id=id;
    this->adjNo = new list<Aresta>;
}

int No::getId(){
    return this->id;
}

list<Aresta>* No::getAdjNo(){
    return this->adjNo;
}

void No::inserirAdj(Aresta aresta){
    adjNo->push_back(aresta);
}

How would the function to carry out the exclusion?

void No::excluirNo(int id){

}

1 answer

1

When I answered the question was different, I don’t think I still have enough information for a different answer from this.

I do not know what is the intention of this code, but starting from the insert which is a simple push_ back(), I think you’re looking for pop_back().

All members of the class.

Some algorithms may be useful. As well as iterators.

  • I simplified all my code so as not to pollute the question. I know of the existence of pop_back() and pop_front(), but they delete exactly the first or last item from the list. In my case I will have a list of objects of this node class, and each object has its id, for example: No1(id=1),No2(id=2),No3(id=3),no4(id=4)... I would like to remove id=2 for example, as I would do?

  • I know there is also remove in the list library, but I don’t know how to use since my parameter is a class, not an integer...

  • What you put in the question indicated that you wanted something else. And with what’s there I don’t even know what [and your real problem. I answered what I gave for what was asked. It is difficult to write one thing and want another.

  • I’m sorry if I wasn’t clear, with what I supplemented it is possible to help me with something ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.