jdog jdog - 6 days ago 5
C++ Question

Inserting an specific item in a Vector

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));
*/

Answer

I want to set/insert a specific element at a specific index

Insert

PetStore 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[indx]->setadopted(true);

Links: enter link description here

Comments