Results 1 to 8 of 8

Thread: No repeted value in QVector

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation No repeted value in QVector

    I ve a class like
    Qt Code:
    1. class mycls
    2. {
    3. QString name;
    4. int rank;
    5. QString type;
    6.  
    7. };
    To copy to clipboard, switch view to plain text mode 


    in a function
    Qt Code:
    1. Fillvalue(int rank, QString type, QString name)
    2. {
    3. mycls *ptr->rank; //Fiiling rank throuu defined set methods of class
    4. ptr->type, ;
    5. ptr->name;
    6. //craete a vector
    7. QVector(mycls *) vtr = value from member function;
    8. if (vtr != NULL && !vtr->contains(ptr))
    9. {
    10. vtr->append(ptr);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    I need to append only the values which are not their in side vector. that means no element should have same name and rank in the QVector.
    But above method is not working fine. Can any bpdy help me??
    Thanks
    Last edited by jpn; 23rd February 2009 at 06:39. Reason: missing [code] tags
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Dec 2007
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: No repeted value in QVector

    Hi this would not obviously work...
    you are checking for just the pointer value.
    what you should be doing here is to iterate through the values and manually check if the name and rank match! QVector has no way of knowing that those two fields are the "primary key" right!!!
    Let your work talk for you

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: No repeted value in QVector

    your code is not syntactically correct in many places. Please fix it so we can grasp what you are trying to do.

    Basically, you have to check if the vector contains an entry with the same rank.
    To check that, you have to iterate over the vector

    Qt Code:
    1. bool has_rank(const QVector<mycls*> &vec, int rank)
    2. {
    3. foreach(const mycls * const ptr, vec)
    4. if (ptr->rank == rank) return true; // assuming you put no NULLs inside
    5.  
    6. return false;
    7. }
    8.  
    9. // use like this
    10. if (!has_rank(vec, ptr->rank))
    11. vec.append(ptr);
    To copy to clipboard, switch view to plain text mode 

    By the way: are you sure you need to store pointers?

    HTH

  4. #4
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: No repeted value in QVector

    have you tried to define operator == on class mycls ?

    Qt Code:
    1. class mycls
    2. {
    3. QString name;
    4. int rank;
    5. QString type;
    6.  
    7. operator == ( const mycls & Other ) const
    8. {
    9. return ( name == Other.name ) && ( rank == Other.rank );
    10. };
    11. };
    To copy to clipboard, switch view to plain text mode 

    This must work, so contains needs operator == defined. If if no one is defined, the algorithm compares full struct ( I don't know if it does member-by-member or simply pointers ). Any of them is an erroneous comparison if you only want to consider name & rank.

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: No repeted value in QVector

    operator== does not help if he is storing pointers instead of values.

  6. #6
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: No repeted value in QVector

    Of course

    I did'nt pay attention to the fact he was using pointers. Sorry.

    Anyway, my sample is a clean way to implement if you don't use pointers.

  7. #7
    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: No repeted value in QVector

    I would use QSet instead of QVector and get rid of pointers in favour of a regular value based structure. Mixing C++ and C-ish code is not really advised.

    Qt Code:
    1. struct MyStruct{
    2. QString name;
    3. int rank;
    4. QString type;
    5. bool operator==(const MyStruct &other) const { return (name==other.name && rank==other.rank && type==other.type); }
    6. bool operator<(const MyStruct &other) const { return ((rank < other.rank) || (rank==other.rank && name < other.name) || (rank==other.rank && name==other.name && type < other.type)); }
    7. };
    8.  
    9. QSet<MyStruct> mySet;
    10. MyStruct str1;
    11. str1 = ...
    12. mySet << str1;
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: No repeted value in QVector

    Thank u caduel

    bool has_rank(const QVector<mycls*> &vec, int rank)
    {
    foreach(const mycls * const ptr, vec)
    if (ptr->rank == rank) return true; // assuming you put no NULLs inside

    return false;
    }

    // use like this
    if (!has_rank(vec, ptr->rank))
    vec.append(ptr);
    I did this way and it worked.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

Similar Threads

  1. using QPair in QVector
    By damonlin in forum Qt Programming
    Replies: 5
    Last Post: 28th January 2009, 09:29
  2. QVector copy constructor
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2008, 17:52
  3. QVector Explosion
    By baray98 in forum General Programming
    Replies: 1
    Last Post: 11th November 2007, 15:12
  4. Memory allocation failure and crash in QVector
    By ashatilo in forum Qt Programming
    Replies: 16
    Last Post: 20th October 2007, 23:27
  5. QVector
    By sabeesh in forum Qt Programming
    Replies: 2
    Last Post: 17th September 2007, 14:37

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.