jeudi 18 juillet 2019

How can I provide multiple iterators for a wrapper container class in C++?

I am really a noob in C++, so please don't mind if my question shows lack of basic knowledge. I have a class User that describes a user in my system. It contains simple fields, like string name, string email, int age, etc. I also have a wrapper class Group that uses an std::list in order to store a collection of Users. What I want to do is to use the Iterator design pattern in order to provide two different iterators for the Group class. By using one of the iterators, I want to be able to go through the list sorted by the User's name and by using the other one I want to do same thing, except that it should be sorted by the User's age.

I read this article (https://www.robertlarsononline.com/2017/04/24/iterator-pattern-using-cplusplus/) on the Iterator design pattern but I'm not sure on how I can adapt the shown code to my case. I am guessing that my Group class needs to have two CreateIterator() methods (i.e: CreateAgeIterator() and CreateNameIterator()), each one returning a different specialization of the Iterator class. The problem is that I don't know where exactly I should put the logic of sorting the list according to a specific criterion. This is just a basic project that I am doing to study a little bit of the language as well as the Iterator design pattern itself. I am not concerned with being STL compliant or anything, I just need a simple implementation of the concept.

Aucun commentaire:

Enregistrer un commentaire