Reputation: 761
I have arrays like:
template<class T> class F
{
...
bool isSet() { return set_; }
T& GetVal() { return val_; }
private:
T val_;
bool set_;
...
}
F<S1> arr1[10];
I'm looking for an iterator (or iterator-like class) to simplify the following:
F<S1>* cur = arr1[0];
while(cur.IsSet())
{
S1* ptr = cur->GetVal();
// do something with ptr
cur++;
};
Would like something a bit cleaner that can work with different types S1, S2, etc..
Upvotes: 1
Views: 1403
Reputation: 6357
you can make your own iterator using the Boost Iterator library, especially iterator_facade
http://www.boost.org/doc/libs/1_49_0/libs/iterator/doc/iterator_facade.html
Upvotes: 3
Reputation: 24133
Really you're looping until you find an F which isn't set.
Use std::find_if
:
#include <algorithm>
#include <iterator>
template<typename Type>
bool doSomethingOrStopIfNotSet(F<Type>& object)
{
const bool isSet = object.isSet();
if(isSet)
{
// do something
}
return !isSet; // return false so that find_if keeps looking
}
int main()
{
F<int> arr[100];
std::find_if(std::begin(arr), std::end(arr), doSomethingOrStopIfNotSet<int>);
}
Upvotes: 2
Reputation: 476950
The standard provides iterators for arrays:
#include <iterator>
T arr[100];
for (auto it = std::begin(arr), end = std::end(arr); it != end; ++it)
{
foo(*it);
it->zap();
}
The type of it
will just be T *
, but this gives you a uniform interface.
The term "iterator" really just refers to a concept rather than any particular piece of code. Naked pointers are perfectly legitimate iterators. What matters is that std::iterator_traits
gives the correct typedefs, which it does for naked pointers.
Upvotes: 8