I’ve got a class, memberlist, that contains a std::list of class memberinfo. These represents the peers on a network.
I use the class to add some functionality to the list.
I want to expose some iterators (begin and end) so that outside code can loop through my internal list and read their data. However, I want to have two ways of doing this – one that includes an element for the localhost, and one that doesn’t.
What’s a good way to do this?
I could put the local node first, then have like begin(showlocal=false) just give the second element instead of the first. Or someone suggested storing a pair of with the bool saying if it’s local or not.
Any suggestions on a good way to do this? I’m not too great on advanced STL stuff yet.
Personally I would approach this in a different way and have your
memberinfohave a way of telling you if it’s local or not.That way you’re not specialising your collection class due to a specialisation of the contained objects. In fact you could just use a standard
std::list<memberinfo>.E.g.
Then you would choose whether you’re interested in local members or not while you’re iterating through the contained objects.
E.g.