[Interest] Help, please !!!

Nikos Chantziaras realnc at gmail.com
Mon Apr 23 20:44:00 CEST 2012


Then you're not doing what you think you're doing:

     QList< QList<int> > listOfLists;
     QList<int> listOfInts;
     listOfInts.append(10);

     listOfLists.append(listOfInts);
     listOfLists[0][0] = 9;

     qDebug() << listOfLists[0][0] << listOfInts[0];

You are modifying a copy, so it prints "9 10" instead of "10 10".  This:

   listOfLists[0][0] = 9;

modifies a copy of listOfInts.  Also the reverse is true.  If you modify 
listOfInts, then the copy of it inside listOfLists is not updated.

"Implicit sharing" means that data is copied when it's modified.  It's 
not a replacement for pointers.


On 23/04/12 21:35, Scott Aron Bloom wrote:
> The following code works just fine...
>
> #include<QList>
> #include<QDebug>
> #include<QCoreApplication>
>
> int main( int argc, char ** argv )
> {
>      QCoreApplication appl( argc, argv );
>
>      QList<  QList<  int>  >  theList;
>
>      for( int ii = 0; ii<  10; ++ii )
>      {
>          QList<  int>  currList;
>          for( int jj = 0; jj<  10; ++jj )
>          {
>              currList.push_back( ii+jj );
>          }
>          theList.push_back( currList );
>      }
>
>      for( int ii = 0; ii<  theList.size(); ++ii )
>      {
>          qDebug()<<  theList[ ii ];
>      }
>
>      for( int ii = 0; ii<  theList.size(); ++ii )
>      {
>          for( int jj = 0; jj<  theList[ ii ].size(); ++jj )
>          {
>              theList[ ii ][ jj ] -= 10;
>          }
>      }
>
>      qDebug()<<  "=====================";
>      for( int ii = 0; ii<  theList.size(); ++ii )
>      {
>          qDebug()<<  theList[ ii ];
>      }
> }
>
>
>
> -----Original Message-----
> From: interest-bounces+scott.bloom=onshorecs.com at qt-project.org [mailto:interest-bounces+scott.bloom=onshorecs.com at qt-project.org] On Behalf Of Scott Aron Bloom
> Sent: Monday, April 23, 2012 11:29 AM
> To: interest at qt-project.org
> Subject: Re: [Interest] Help, please !!!
>
> Not true at all..
>
> -----Original Message-----
> From: interest-bounces+scott.bloom=onshorecs.com at qt-project.org [mailto:interest-bounces+scott.bloom=onshorecs.com at qt-project.org] On Behalf Of Nikos Chantziaras
> Sent: Monday, April 23, 2012 10:44 AM
> To: interest at qt-project.org
> Subject: Re: [Interest] Help, please !!!
>
> Note that with QList<  QList<int>  >  you can't modify the other lists.
> You'd only be modifying the copies.
>
> On 23/04/12 20:42, Scott Aron Bloom wrote:
>> Be careful with
>> List->append(&someOtherList );
>>
>> If someOtherList goes out of scope, you will be pointing to a deleted list.
>>
>> Since QList uses implicit sharing, the cost of removing the inner pointer isn't that much.
>>
>> QList<   QList<   int>   >
>>
>> Is much safer
>>
>> Scott
>>
>>
>>
>> -----Original Message-----
>> From: interest-bounces+scott.bloom=onshorecs.com at qt-project.org [mailto:interest-bounces+scott.bloom=onshorecs.com at qt-project.org] On Behalf Of Nikos Chantziaras
>> Sent: Monday, April 23, 2012 10:34 AM
>> To: interest at qt-project.org
>> Subject: Re: [Interest] Help, please !!!
>>
>> On 23/04/12 20:17, Miguel Milán Isaac wrote:
>>> I need to use QList<QList<int>    *>    * list; but I do not know how to
>>> initialize it.
>>>
>>> I want to do something like this:
>>> if (! listRemoved->    at (pos) ->    contains (id)) {
>>>         //do something
>>> }
>>
>>      QList<   QList<int>*>* list = new QList<   QList<int>*>;
>>
>>      // Fill it with 10 elements.
>>      for (int i = 0; i<   10; ++i) {
>>          list->append(new QList<int>);
>>          // Or:
>>          // list->append(&someOtherList);
>>      }
>>
>> Now every list[i] (or list->at(i)) gives you a QList<int>*.  For example:
>>
>>      if (!list->at(pos)->contains(id)) {
>>          // Do something.
>>      }
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest





More information about the Interest mailing list