Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOOST_FOREACH and a vector

I have a vector of Scenes, vector<Scene>. What is the correct way to iterate over the elements, as reference or not?

For example this:

BOOST_FOREACH(Scene scene, mScenes)
{
       .....
}

Does the macro copy the Scene for each iteration over the vector, or does it use a reference behind the scenes?

So is it any different from this:

BOOST_FOREACH(Scene& scene, mScenes)
{
       .....
}
like image 347
KaiserJohaan Avatar asked Aug 01 '26 05:08

KaiserJohaan


2 Answers

BOOST_FOREACH behaves exactly as you tell him, by value, reference or const reference

like image 53
kassak Avatar answered Aug 03 '26 22:08

kassak


Your first example does copy each Scene. Most likely, you want the second. With the first, you can modify scene and the vector will be unaffected. If you're not modifying the vector, you should use const Scene& scene. If you are, use Scene &.

like image 22
David Schwartz Avatar answered Aug 03 '26 21:08

David Schwartz