Reputation: 54183
Basically I want to reverse iterate through 2 std::vectors. a layer has a vector of shapes.
usually I could do something like this:
for(int i = 0; i < layers.size(); ++i)
{
for(int j = 0; j < layers[i].shapes.size(); ++j)
{
layers[i].shapes[j].dosomething();
}
}
However right now I need to reverse iterate through the vectors and therefore need to use a reverse iterator but how would this work? Since iterators kind of make it like a for each, how can I acces the current shape of the current layer being iterated through? Thanks
Upvotes: 2
Views: 187
Reputation: 490663
You don't really need iterators (reverse or otherwise) for this. If you like the code you have right now, and just want it to run in reverse, you can do that fairly directly:
for (int i=layers.size()-1; i>-1; --i)
for (int j=layers[i].shapes.size()-1; j>-1; --j)
layers[i].shapes[j].dosomething();
Of course, you can use iterators if you prefer, but in this case I think it'll make the code longer and more complex overall, without adding much:
std::vector<shape>::reverse_iterator this_shape;
std::vector<layer>::reverse_iterator this_layer;
for (this_layer = layers.rbegin(); this_layer != layers.rend(); ++this_layer)
for (this_shape = this_layer->shapes.rbegin(); this_shape != this_layer->shapes.rend(); ++this_shape)
this_shape->dosomething();
Upvotes: 0
Reputation: 69772
Use reverse iterators.
typedef std::vector::<Layer>::reverse_iterator LayerIt;
for(LayerIt layerIt = layers.rbegin(); layerIt != layers.rend(); ++layerIt) // reverse-iterator
{
Layer& layer = *layerIt;
typedef std::vector<Shape>::reverse_iterator ShapeIt;
std::vector<Shape>& shapes = layer.shapes;
for(ShapeIt shapeIt = shapes.rbegin(); shapeIt != shapes.rend(); ++shapeIt) // reverse-iterator
{
Shape& shape = *shapeIt;
shape.dosomething();
}
}
See std::vector's rbegin() and rend() functions : http://www.cplusplus.com/reference/stl/vector/ I decomposed the code to make it more obvious, tell me if it's not. If you don't know about iterators, you'll have to search :)
Notice that if you use a recent compiler with the new auto
features, it's simpler to write :
for(auto layerIt = layers.rbegin(); layerIt != layers.rend(); ++layerIt) // reverse-iterator
{
Layer& layer = *layerIt;
std::vector<Shape>& shapes = layer.shapes;
for(auto shapeIt = shapes.rbegin(); shapeIt != shapes.rend(); ++shapeIt) // reverse-iterator
{
Shape& shape = *shapeIt;
shape.dosomething();
}
}
Upvotes: 1
Reputation: 41862
The easy way:
for(int i = layers.size() - 1; i >= 0; --i)
{
for(int j = layers[i].shapes.size() - 1; j >= 0; --j)
{
layers[i].shapes[j].dosomething();
}
}
The "proper" way:
for(vector<Layer>::reverse_iterator i = layers.rbegin(); i != layers.rend(); ++i)
{
for(vector<Shape>::reverse_iterator j = i->shapes.rbegin(); j != i->shapes.rend(); ++j)
{
j->dosomething();
}
}
Upvotes: 5
Reputation: 4958
Iterators to containers allow you to access the "current" object using the the * or -> operators - in that sense iterators are like pointers to the vector's elements.
For instance, if iter
is an iterator on a vector of layers then you can access the layer currently pointed by the iterator using:
layer = *(iter);
or
nshapes = iter->shapes.size();
Upvotes: 0
Reputation: 22493
You can use reverse iterators too, but it gets a bit verbose. If you have Boost, though, it's really easy as you can do something like:
BOOST_REVERSE_FOREACH( std::vector<Shape>& layer, layers )
{
BOOST_REVERSE_FOREACH( Shape& shape, layer )
{
shape.dosomething();
}
}
Upvotes: 0
Reputation: 147036
for(std::vector<mytype>::reverse_iterator i = layers.rbegin(); i != layers.rend(); ++i)
{
for(std::vector<myothertype>::reverse_iterator j = i->shapes.rbegin(); j != i->shapes.rend(); ++j)
{
j->dosomething();
}
}
Iterators are more flexible this way than index-based - it's far simpler to change begin/rbegin, end/rend, and reverse_iterator/iterator,to change the iteration direction. This becomes even easier if you use this code often and can just stick a typedef somewhere for it and definitely best if you've got C++0x or can use an inline function for the automatic type deduction.
Upvotes: 0
Reputation: 4534
If you're using indices for forward iteration why not for reverse? ie
for( int i = layers.size()-1; i>=0; --i )
Upvotes: 0