Is there a way for an iterator to return an object in each element of a C++ Standard Library vector?
I have this code:
struct mystruct {
int field1;
}
int DoSomethingWithMyStruct(mystruct& a);
std::vector<mystruct> myVector;
std::vector<mystruct>::iterator it;
mystruct s1,s2, temp;
s1.field1=1;
s2.field1=2;
for (it=myVector.begin();it!=myVector.end();it++)
{
//I want to call DoSomethingWithMyStruct, so I have to pass in mystruct object.
//can I use iterator to get the object of each element in myVector without having to create a temporary mystruct object and pass it in?
//I'm looking for an easier way than having to do this:
temp.field1 = it->field1;
DoSomethingWithMyStruct(temp);
}
As well as what the others said, you can do this instead:
It’s short and succinct. No need of manual loop.