Moonlit
Moonlit

Reputation: 5409

Delete custom object from vector

I have the following code to store objects of type LVA in a vector. I've implemented a method for adding a LVA Object to the vector. First question: Is it okay if i use a reference to the LVA object in this method?

Now i want to implement a method for the deletion of a LVA object. I want to have the following method signature: void RemoveLVA(LVA& lva). How can I implement this method? i.e. How can i find the right object in the vector to delete it?

Manager.h

class Manager {
public:
    Manager();
    Manager(const Manager& orig);
    virtual ~Manager();
    vector<LVA> GetLvas() const;
    void AddLva(LVA& lva);
private:
    vector<LVA> lvas;

};

Manager.cpp:

#include "Manager.h"

Manager::Manager() {
}

Manager::Manager(const Manager& orig) {
}

Manager::~Manager() {
}

vector<LVA> Manager::GetLvas() const {
    return lvas;
}

void Manager::AddLva(LVA& lva) {
    lvas.push_back(lva);
}

Upvotes: 1

Views: 2147

Answers (6)

alexbuisson
alexbuisson

Reputation: 8469

Yes it's Ok to pass the LVA object as ref in your add and remove method. You could also declare those parameters as 'const' because I suppose add and remove will not affect the object .

for the remove method

void RemoveLVA(const LVA& lva)
{
    std::vector<LVA>::iterator position =
    std::find(lvas.begin(), lvas.end(), lva);
    if (position != lvas.end())
    {
        lvas.erase(position);
    }
}

but LVA class must have an == operator.

good luck.

Upvotes: 0

John Dibling
John Dibling

Reputation: 101456

Yes, AddLva can take a reference, although it would typically take a const reference:

void Manager::AddLva(const LVA& lva) {
    lvas.push_back(lva);
}

Note however that if you are expecting that the reference is added to the vector, this isn't doing that. A copy of the object lva refers to is made, and that copy is added to the vector.

Upvotes: 0

James Kanze
James Kanze

Reputation: 153919

Without knowing more about LVA, it's hard to be precise, but you probably want a const reference for AddLva, since you're not modifying it in the function (and you may want to pass a temporary).

For removal: you'll have to define some sort of equivalence function over LVA is you want to remove a matching element. This can be LVA::operator==, or some other object or function: in the first case, you use std::find to find the position, and in the second, std::find_if. If the vector can contain more than one matching element, you might want to look into std::remove or std::remove_if.

Upvotes: 1

chrisaycock
chrisaycock

Reputation: 37930

Is it okay if i use a reference to the LVA object in AddLva(LVA& lva)?

Yes, std::vector will have a copy of your original object after push_back() is complete.

How can I implement RemoveLVA(LVA& lva)?

You will need to find the object in your vector. (You may use std::find() if operator==() is defined for LVA.) Then invoke the erase() function for your vector.

Upvotes: 1

rems4e
rems4e

Reputation: 3172

Your insertion method is right, but don't forget that only a copy from your LVA object is inserted in the vector. it means than in the RemoveLVA method, you will need to use somthing like an operator== overload to std::find the vector's object which matches your parameter, and then deleting it.

You may prefer to insert pointers or std::shared_ptr of LVA in your vector. No object copy will then occur, and the search of an object will fall back to a comparison of pointer, given you keep the pointer somewhere else in your program (2 objects with same contents and different stack or heap locations would then compare to false).

Upvotes: 0

Mahesh
Mahesh

Reputation: 34625

Is it okay if i use a reference to the LVA object in this method?

No problem. You are actually doing a copy by a push_back operation.

Upvotes: 1

Related Questions