i got error when try :
how to solve thisQt Code:
QList<double> zz; double r=.05; for (int k = 0; k < 3; ++k) { zz[0] +=r; }To copy to clipboard, switch view to plain text mode
thanks
i got error when try :
how to solve thisQt Code:
QList<double> zz; double r=.05; for (int k = 0; k < 3; ++k) { zz[0] +=r; }To copy to clipboard, switch view to plain text mode
thanks
In general it would be helpfull if you would actually paste the error message!
As far as I can see, you have to fill the list first. [] can only be used if that index allready exists.
you have to fill the list first. []
yes i did that by :
but that is idiotzz<<0.0<<0.0<<0.0<<0.0
There is no need to inquire not my nature abuse literature with one .
I meant the Behavior of the QList
that is all
What behaviour of QList? That you can't operate on items you don't have? Every list works this way. It's a list with finite number of elements.
but in c# for example you don't obligate to initialize initial values :
this is similar to QList and works fluently without any initializationdoble zz=3;
double[] foo = new double[4];
for (int k = 0; k <10; ++k)
{
foo[0] += zz;
}
No, you are wrong. This is the initialization of the array:
You explicitly set the array size to four elements filled with possibly random data.Qt Code:
double[] foo = new double[4];To copy to clipboard, switch view to plain text mode
The equivalent for Qt (with the difference that each cell is initialized to 0.0) is:
alrawab (12th January 2013)
yes but this not applicable For QList as i know ?
is there any replacement for zz<<0.0<<0.0<<0.0<<0.0;
it's looks ugly
use QVector
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
It's a list. Initializing it like I did with a vector is slow and unnecessary.
e.g.is there any replacement for zz<<0.0<<0.0<<0.0<<0.0;
Qt Code:
for(int i=0;i<4;++i) zz.append(0);To copy to clipboard, switch view to plain text mode
By the way, if you know your array of doubles is going to be always of size 4, use a regular C array instead.
If the object is to always have a fixed number of elements then you have better choices than QList as others have pointed out. If your QList must start with a certain number of elements but may grow, and you really find the simple initialisation loop so distasteful, then I guess you can use QVector to initialise your QList:
I don't think this is an improvement over the other options. Ultimately expecting C++ and Qt to behave like C# and its library is like tilting at windmills.Qt Code:
QList<double> stuff = QList<double>::fromVector(QVector<double>(4, 0.0));To copy to clipboard, switch view to plain text mode
alrawab (14th January 2013)
With C++11 and Qt 5 one can also use initializer lists.
Qt Code:
QList<double> xxx = { 0.0, 0.0, 0.0, 0.0 }; // with C++11 enabledTo copy to clipboard, switch view to plain text mode
alrawab (14th January 2013)
Bookmarks