How to create array of objects at runtime.
-
Hi All
In my project I need to read a file and each line in a file consists of two values and these two values needs to be stored in the object, and all the object need to be inserted into the vector.
My file is something like this:
4,5
6,5
...
...
...so for first line I have to create an object and store the value 4 and 5 in private member of the class.
I don't know how many lines are there in my file. As per my understanding I have to create an array of objects (object for each line) at runtime and store the values in it and intsert these objects in a vector.
Please let me know if anybody can help me on this.
-
Mmm, QList is not enough for you here?
-
Hello Denis
Sorry, I have not used QT much.
Can you please give me some example, how to use QList in this situationThanks!!!
-
Please, look "here":http://doc.qt.nokia.com/4.7/qlist.html . You will find a bunch of examples there.
-
Hello Denis
I still have a doubt....
After reading the data(line by line) from file, I have to store them in my class member variables for each object and move this object in QVector... So how can i do this using QList?
-
[quote author="agupta" date="1311226392"]Hi All
In my project I need to read a file and each line in a file consists of two values and these two values needs to be stored in the object, and all the object need to be inserted into the vector.
My file is something like this:
4,5
6,5
...
...
...
[/quote]
First you have to decide whether you need a QList or a QVector. In your case, I'd probably prefer a QVector. See here fore more details: http://doc.qt.nokia.com/4.7-snapshot/containers.htmlNow, there is a nice handy thing called QPair http://doc.qt.nokia.com/latest/qpair.html.
In your example, simply do:
@
QVector< QPair<int, int> > myPairVec;// if you know the number of lines in your file, pre-allocate
// the vector for best speed:
myPairVec.reserve(numLines);// now add to the vector
for(int i = 0; i < numLines; ++i) {
// read line from file
// parse line
// put values into integers x and y// now we can add to the vector
myPairVec.append(qMakePair(x, y));
}//end for// if you want to access elements, do so by
cout << "first x = " << myPairVec.at(0).first << endl;
cout << "last y = " << myPairVec.last().second << endl;
@ -
Oh, and you could've done the same thing with a QList of course. The difference is that QList has one more level of "indirection", whereas QVector stores its element in one continuous memory block. This influences performance (good or bad, depends). But for your case: Probably doesn't matter.
qMakePair is a useful helper that will create a QPair<T1, T2>, based on the types of the arguments...
So you wouldn't have to create an object. On the other hand, the members .first and .second are public, and in your first post you said you needed private members...
-
[quote author="LinusA" date="1311228084"]So you wouldn't have to create an object. On the other hand, the members .first and .second are public, and in your first post you said you needed private members...[/quote]
Of course, a QPair can always be used as a private member variable in an object too, should that use case be appropriate.
-
-
agupta, could you tell us a bit more about this object you need to store your values in? Is it an object you already have? If so, what does it look like? Note that not all objects are suitable to store as value in a container. You may need to store a pointer to the object on the heap instead. QObject and everything derived from it is such a type of object...
-
i have two tables internaldelivery and products.in internaldelivery table(pid,pname,qty,unit,preparedby,givento,date)is there.i have inserted (pname,qty,unit,preparedby,givento,date)in internaldelivery table.in internaldeliverytable (pname) and also in products table (name) is same.how can i get the pid value to insert the internaldeliverytable. Please find and say the solution for me.
-
anybody say that qt is end or not
-
[quote author="saravanavelu" date="1378978443"]anybody say that qt is end or not[/quote]
I have no clue what you are asking, or how it is relevant to this topic.
-
[quote author="saravanavelu" date="1378978443"]anybody say that qt is end or not[/quote]
Hi saravanavelu,
Do you mean that Qt is dead or still working. If that is you question, then
let me tell you that Qt is now acquired by Digia from Nokia. AFAIK Qt is now one of the most used IDE in the world. Qt is has released its new version, ie Qt 5.1.1 recently. Also developments are going on different OS like Android, iOS, Linux, Windows and many more.
Many companies are nowadays using Qt as their development platforms.
Check the Qt website and you can find lots of changes have occurred in the recent years.Regards
Ansif