I'm new to C++ and I am having problems adapting from Java. I know that Vectors in C++ work similar to ArrayList in Java, but I am having some problems understanding it completely.
Let's say I have a vector<PetStore*> pets
, and each PetStore
object has some getters and setters. I want to set/insert a specific element at a specific index in the vector. I also want to call the method bool setAdopted(bool adopted)
on the inserted pet and set adopted to true
.
/*
Pseudo code
pets.insert(pets.begin()+i, setAdopted(true));
*/
I want to set/insert a specific element at a specific index
Insert
PetStore *item;
item = new PetStore();
vector<PetStore*> pets;
vector<PetStore*>::iterator it;
it = pets.begin();
it = pets.insert ( it , item );
// another way..
pets.insert (it,2,item);
Set:
pets.at(indx)->setadopted(true);
Links: vector/insert