Reputation: 23674
I'm used to other languages that support foreach loops or the range-based for in c++11. I'm trying to emulate it in c++98.. Mainly because I don't like using the (*it)
notation every time I need the object.
Say I have this c++11 code:
std::vector<int> myVec{...};
for(auto& outerLoopVar : myVec)
{
for(auto& innerLoopVar : myVec)
{
// do logic
}
}
Is there a better way to replicate that in c++98 than doing something like this?
std::vector<int> myVec;
// populate vector
for(std::vector<int>::iterator it1 = myVec.begin(); it1 < myVec.end(); it1++)
{
int& outerLoopVar = *it1;
for(std::vector<int>::iterator it2 = myVec.begin(); it2 < myVec.end(); it2++)
{
int& innerLoopVar = *it2;
// do logic
}
}
Upvotes: 1
Views: 84
Reputation: 27577
You want to use boost foreach:
#include <boost/foreach.hpp>
BOOST_FOREACH(int& elem, myVec) {
// do logic
}
Upvotes: 2
Reputation: 1765
Not really, I mean the iterator notation is quite standard and is applicable to multiple containers and objects. Its a valuable tool which should be used. I don't really know why you don't like using the notation but I think its better if you embrace it.
Upvotes: 1