[Solved] Iterator over custom class



  • Hi all.

    I'd to know how to iterate over a custom class. With Qt or STL.

    The code is more or less shown below:
    @
    class Messages{
    public:
    Messages(){}
    QList<QString> mymessages;
    };

    class PointList{
    public:
    PointList(){}
    QList<int> mypointlist;
    Messages mymessages;
    int somenumber;
    };

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    PointList mypl;
    
    for(int i = 0; i<10; i++){
        mypl.mypointlist.append(i);
        mypl.mymessages.mymessages.append(QString::number(i));
    }
    
    QList<int>::const_iterator i;
    for (i = mypl.begin(); i != mypl.end(); ++i)
        qDebug() << *i;
    
    return a.exec&#40;&#41;;
    

    }@

    As you can see, I want to iterate over a QList wrapped in a class. To use iterator how I must implement begin, end and operator++, operator!=.
    Is there any example on Qt documentation (I haven't found jet) about this situation?
    Looking at Qlist source code, is really complicated to do. Too much work for something that can be solved passing the reference.

    Finally, to iterate over Messages class I must pass the reference of the QList to main. No other way.

    I know is easier passing reference of QList<int> mypointlist; to main and iterate. But I think isn't a good practice, is it?

    What do you recommend me?


  • Moderators

    If a class (like PointList) holds a couple of things (points and messages) then it's not really a point list, is it?

    If you want to easily iterate over them using only mypl variable this is one way(among many) to do it:
    @
    class Messages{
    public:
    Messages(){}
    QStringList mymessages;

    typedef QStringList::const_iterator const_iterator;
    const_iterator cbegin() const {
              return mymessages.cbegin(); }
    const_iterator cend() const {
              return mymessages.cend(); }
    

    };

    class PointList{
    public:
    PointList(){}
    QList<int> mypointlist;
    Messages mymessages;
    int somenumber;

    typedef QList<int>::const_iterator const_iterator;
    const_iterator cbegin() const {
              return mypointlist.cbegin(); }
    const_iterator cend() const {
              return mypointlist.cend(); }
    
    Messages::const_iterator messagesCbegin() const {
              return mymessages.cbegin(); }
    Messages::const_iterator messagesCend() const {
              return mymessages.cend(); }
    

    };

    //and then...
    PointList::const_iterator i = mypl.cbegin();
    for (; i != mypl.cend(); ++i)
    qDebug() << *i;

    Messages::const_iterator j = mypl.messagesCbegin();
    for (; j != mypl.messagesCend(); ++j)
    qDebug() << *j;
    @



  • Thanks Chris for you answer.

    I called PointList, but the real name isn't PointList. Look as a List of points with more information not related with the list.

    Anyway, thanks for you answer.

    I found more results searching as "custom iterator" on Google.
    In "this link":https://sites.google.com/site/andreatagliasacchi/blog/customforeachiteratorsinqt show how to make a custom "foreach".

    Also I foundt different solutions using stl, all more complicated than yours.
    I'll try with a in/out iterator as well.

    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.