Results 1 to 6 of 6

Thread: using QPair in QVector

  1. #1
    Join Date
    Oct 2008
    Location
    Taiwan
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default using QPair in QVector

    Dear All:
    I use QPair in QVector when developing .
    But there's some problem.
    here is text example:

    Qt Code:
    1. void test()
    2. {
    3. QVector< QPair< int , QString > > first;
    4. QVector< QPair< int , QString > > second;
    5.  
    6. for(int iIdx = 0; iIdx <5 ; ++iIdx )
    7. first.push_back(QPair< int , QString >(iIdx , "aaa") );
    8.  
    9. qCopy(first.begin(), first.end(), second.begin()); // it occurred errors
    10. }
    To copy to clipboard, switch view to plain text mode 

    Is there any suggestion for me if I would like to copy from one vector to another ??
    Thank you~~
    Last edited by jpn; 28th January 2009 at 09:15. Reason: missing [code] tags

  2. #2
    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: using QPair in QVector

    What exact error are you getting?

  3. #3
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using QPair in QVector

    qCopy assumes that the target elements allready exist and then overwrites them.

    So this example must be:

    Qt Code:
    1. void test()
    2. {
    3. QVector< QPair< int , QString > > first;
    4.  
    5. for(int iIdx = 0; iIdx <5 ; ++iIdx )
    6. first.push_back(QPair< int , QString >(iIdx , "aaa") );
    7.  
    8. QVector< QPair< int , QString > > second(first.count());
    9.  
    10. qCopy(first.begin(), first.end(), second.begin());
    11. }
    To copy to clipboard, switch view to plain text mode 

    Maybe this would be better:

    Qt Code:
    1. void test()
    2. {
    3. QVector< QPair< int , QString > > first;
    4.  
    5. for(int iIdx = 0; iIdx <5 ; ++iIdx )
    6. first.push_back(QPair< int , QString >(iIdx , "aaa") );
    7.  
    8. QVector< QPair< int , QString > > second(first);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Because it can share data until a write operation is made on either vector.
    Last edited by seneca; 28th January 2009 at 09:02.

  4. #4
    Join Date
    Oct 2008
    Location
    Taiwan
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using QPair in QVector

    Dear wysota:
    There's a exception error at run time~~
    And it broke at QBasicAtomicInt::deref()...

    Dear seneca:
    In my program, all I need to do is copy member to another member,
    It means I have to use qCopy rather than constructor, does it ??

    thank you~~

  5. #5
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using QPair in QVector

    Using the copy constructor will have the same effect in practise, however the data stay shared as long as you don't change anything on either vector. As soon as you change something in the data, the data is physically copied behind the scenes and you continue with double memory.

    Because this is completely transparent your application needs not wory about this. It is the same mechanism as when you copy a QString for example.

  6. The following user says thank you to seneca for this useful post:

    damonlin (28th January 2009)

  7. #6
    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: using QPair in QVector

    Seneca is right - you try to overwrite an unexisting item. You want to add items, not overwrite them.

Similar Threads

  1. QVector copy constructor
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2008, 17:52
  2. Problem with qvector
    By zorro68 in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2008, 01:02
  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.