Question

How can i edit a object in a vector?

Right now my vector is vector<PCB> M3M;

Inside it are a few objects from my class below.

class PCB
{
public:

    void setPID (int a)
    {
        PID = a;
    }
    int retrievePID()
    {
        return PID;
    }

    int retrieveLimit()
    {
        return Limit;
    }
    void setLimit (int a)
    {
        Limit = a;
    }
    int retrieveBase()
    {
        return Base;
    }
    void setBase (int a)
    {
        Base = a;
    }
    int retrieveHoleTrueOrFalse()
    {
        return HoleTrueOrFalse;
    }
    void setHoleTrueOrFalse (int a)
    {
        HoleTrueOrFalse = a;
    }

private:
    int PID;
    int Limit;
    int Base;
    int HoleTrueOrFalse;
};

How would i be able to edit the PID part in any place i choose in my object?

For example i want to set a new PID in my vector at M3M[4]. How do i go about doing that?

Was it helpful?

Solution

M3M[4].setPID(<new PID>);

OTHER TIPS

You also can have an iterator that can point to an element in your array and set a new PID to that. Example:

std::vector<PCB>::iterator it = M3M.begin();
//this will advance your iterator by 3
it += 3;
it->setPID(5); // this will set the 3rd element's PID to 5

Keep in mind that your vector needs to have at least 4 elements in order for the code above to execute. In order to you fill your vector, you can use push_back() method of your vector.

M3M.push_back(PCB(0));
M3M.push_back(PCB(1));
M3M.push_back(PCB(2));
M3M.push_back(PCB(3));
M3M.push_back(PCB(4));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top