because im looping through each vector of an object from a vector
heres the one that returns vector which i think is slow, since it has to copy
the vector of bullets to another vector here std::vector<Bullet> b = t[a].getBullets();
class Bullet
{
}
class Tower
{
const std::vector<Bullet>& getBullets()
{
return b;
}
std::vector<Bullet> b;
}
std::vector<Tower> t;
//game loop
for ( int a = 0; a < t.size(); a++ )
{
std::vector<Bullet> b = t[a].getBullets();
for ( int c = 0; c < b.size(); c++ )
{
////// process
}
}
and heres the second one that returns an iterator to be used in the loop:
Given that getBullets is returning a reference to t[a]'s member, you could keep referencing it without creating a local copy:
const std::vector<Bullet>& b = t[a].getBullets();
(just keep in mind that the reference will be invalidated by any structural changes, such as a push_back, made to t[a].b, and possibly changes made to t as well)
Returning a pair of iterators or another form of view is perfectly reasonable too (and those iterators will be invalidated similarly, so don't let them sit around). In fact, if you name your iterator-returning member functions "begin()" and "end(), you will be able to use range-for: