Results 1 to 11 of 11

Thread: Problem regarding C++ STL

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Problem regarding QPtrList::findRef

    Hi guys,
    I'm working on a project where I need to remove Qt libraries and use C++ STL.
    I need to replace QPtrList::findRef defined in Qt with C++ STL alternative . But unable to get any solution.

    For example
    I have used STL's find(InputIterator first, InputIterator last, const T& value) to replace QPtrList::find. The code can be seen as below

    Defined in Qt3
    Qt Code:
    1. if( -1 == steps.find( s ) )
    To copy to clipboard, switch view to plain text mode 

    Alternative in C++ STL
    Qt Code:
    1. StepListIterator sIt = find( steps.begin(), steps.end(), s);
    2. if (sIt == steps.end())
    3. {
    4. //not found perform relevant operation
    5. }
    To copy to clipboard, switch view to plain text mode 

    Similar way I need to replace Qt's QPtrList::findRef with an C++ STL alternative.
    Please help me

    Waiting for your reply

    Thankx in advance

  2. #2
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Problem regarding C++ STL

    Hi guys,
    I'm working on a project where I need to remove Qt libraries and use C++ STL.
    I need to replace QPtrList::findRef defined in Qt with C++ STL alternative . But unable to get any solution.

    For example
    I have used STL's find(InputIterator first, InputIterator last, const T& value) to replace QPtrList::find. The code can be seen as below

    Defined in Qt3
    Qt Code:
    Qt Code:
    1. if( -1 == steps.find( s ) )
    To copy to clipboard, switch view to plain text mode 

    Alternative STL Code:
    Qt Code:
    1. StepListIterator sIt = find( steps.begin(), steps.end(), s);
    2. if (sIt == steps.end())
    3. {
    4. //not found perform relevant operation
    5. }
    To copy to clipboard, switch view to plain text mode 
    Similar way I need to replace Qt's QPtrList::findRef with an C++ STL alternative.
    Please help me
    This is really improtant to me

    Waiting for your reply

    Thankx in advance

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem regarding C++ STL

    What is "s"? If "s" is a pointer, then the implementation you provided is the implementation of findRef and not Find.

  4. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem regarding C++ STL

    Hi, thankx for your reply

    Here are the declarations given
    in Qt
    Qt Code:
    1. typedef QList<SynStep> SynStepList;
    2. typedef QListIterator<SynStep> StepListIterator;
    3. SynStep* s; //where SynStep is a class defined by me
    4. class StepList : public SynStepList
    5. {
    6.  
    7. };
    8. StepList steps;
    9. if( -1 == steps.find( s ) )
    To copy to clipboard, switch view to plain text mode 

    in STL
    Qt Code:
    1. typedef std::vector<SynStep*> SynStepList;
    2. typedef std::vector<SynStep*>::iterator StepListIterator;
    3. SynStep* s; //where SynStep is a class defined by me
    4.  
    5. class StepList : public SynStepList
    6. {
    7.  
    8. };
    9. StepList steps;
    10. StepListIterator sIt = find( steps.begin(), steps.end(), s);
    11. if (sIt == steps.end())
    12. {
    13. //not found perform relevant operation
    14. }
    To copy to clipboard, switch view to plain text mode 
    Ok if that is the implementation for FindRef than can you please tell me how can I
    implement Find....

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem regarding C++ STL

    But these declarations are from Qt4 and in the first post you gave Qt3 code.

    QPtrList::find compares items referenced by pointers, findRef compares pointers directly, therefore if you want to compare items, you have to convince STL to compare items. I doubt it is directly possible so you'll probably have to subclass or at least create a custom compare functor and tell find() to use it.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem regarding C++ STL

    There is no difference between find() and findRef() if you have a vector of pointers.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem regarding C++ STL

    Threads merged.

    Please don't ask the same question in different section.

  8. #8
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem regarding C++ STL

    Quote Originally Posted by jacek View Post
    There is no difference between find() and findRef() if you have a vector of pointers.

    Of course there is.
    Diffrenent pointers can point to different objects that have same value.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem regarding C++ STL

    Quote Originally Posted by bood View Post
    Of course there is.
    Diffrenent pointers can point to different objects that have same value.
    But still, if the value is a pointer, there is no difference, since find() doesn't dereference pointers.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem regarding C++ STL

    Quote Originally Posted by jacek View Post
    But still, if the value is a pointer, there is no difference, since find() doesn't dereference pointers.
    I guess that for Qt3 with QPtrList it might make a difference. Of course STL doesn't support dedicated lists for pointers, so to port the same behaviour to STL, one has to provide a custom compare functor.

  11. #11
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem regarding C++ STL

    Quote Originally Posted by wysota View Post
    I guess that for Qt3 with QPtrList it might make a difference. Of course STL doesn't support dedicated lists for pointers, so to port the same behaviour to STL, one has to provide a custom compare functor.
    Can you please visualize with an example??

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.