PDA

View Full Version : How to store two <T> in a list?



pipapongo
10th April 2013, 17:37
hi,
i am looking for something to strore two strings in a list without having to sublass it ( create a class with two strings and making another class with a list object with the first class as items)

i found QHash and QPair, but i don't need the key funktion

just looking for
somethingList(T1 value, t2 value)

thanks in advance

amleto
10th April 2013, 23:06
Right, so how about qlist of qpair

giblit
10th April 2013, 23:07
if you need to store two stings in a list you can do
QList<QString> stringList;
stringList << QString1 << QString2;
//or this
QStringList stringList;
QStringList << QString1 << QString2;
//or even
QString stringList[2] = {"QString1", "QString2"};

ChrisW67
10th April 2013, 23:54
hi,
i am looking for something to strore two strings in a list without having to sublass it ( create a class with two strings and making another class with a list object with the first class as items)
A generic approach as suggested by Amleto


QList<QPair<QString,QString> > list;
list << QPair<QString,QString>("A", "B");
qDebug() << list.at(0).first << list.at(0).second;

but this is ultimately more readable because the names can be meaningful:


struct Name1 {
Name1(const QString &a, const QString &b):
name2(a), name3(b)
{ }

QString name2;
QString name3;
};

QList<Name1> list;
list << Name1("A", "B");
qDebug() << list.at(0).name2 << list.at(0).name3;

There's no second user-defined class involved in the second option. I have no idea why your "no subclass" requirement is useful.

pipapongo
11th April 2013, 08:38
thanx for all the aswers.
@amleto
i dont neet the key function and i want the list to be ordere in the way i enter values
does.that work with qlist of qpair ?

BalaQT
11th April 2013, 09:49
hi,

i dont neet the key function and i want the list to be ordere in the way i enter values. does.that work with qlist of qpair ?
Yes that will store in order.

Check with ChrisW67 reply. it can be accessed by index 0,1...

As Chris's suggestion, the class approach will be cleaner to read

one more thing,

i found QHash and QPair, but i don't need the key funktion
In QHash u cannot store in order.

@glib, He need to store the strings in pair. ex ("str1", "str2"),("str3",str4") in other words,he need to store 2dimension template.
A normal QStringList or QList<QString> will not help.

Bala