How to efficiently create a QList with a single item in one statement?
-
How do I create a QList containing a single item in a way I can use as input value for a method, e.g.
QHash<int,QList<int>> listHash; listHash.insert(0,QList<int>(???)); //What kind of constructor or static creation method can I use here?
With std::vector it's easy:
QHash<int,std::vector<int>> vectorHash; listHash.insert(0,std::vector<int>(1,5)); // Create vector with 1 item of value 5
However, I need to stick to an existing interface, so I have to use QList in my case...
-
How do I create a QList containing a single item in a way I can use as input value for a method, e.g.
QHash<int,QList<int>> listHash; listHash.insert(0,QList<int>(???)); //What kind of constructor or static creation method can I use here?
With std::vector it's easy:
QHash<int,std::vector<int>> vectorHash; listHash.insert(0,std::vector<int>(1,5)); // Create vector with 1 item of value 5
However, I need to stick to an existing interface, so I have to use QList in my case...
-
How do I create a QList containing a single item in a way I can use as input value for a method, e.g.
QHash<int,QList<int>> listHash; listHash.insert(0,QList<int>(???)); //What kind of constructor or static creation method can I use here?
With std::vector it's easy:
QHash<int,std::vector<int>> vectorHash; listHash.insert(0,std::vector<int>(1,5)); // Create vector with 1 item of value 5
However, I need to stick to an existing interface, so I have to use QList in my case...
@asperamanca QList only offers the initializer list, QVector on the other hand, offers, like std::vector , a QVector(int size, const T &value) overload.
-
How do I create a QList containing a single item in a way I can use as input value for a method, e.g.
QHash<int,QList<int>> listHash; listHash.insert(0,QList<int>(???)); //What kind of constructor or static creation method can I use here?
With std::vector it's easy:
QHash<int,std::vector<int>> vectorHash; listHash.insert(0,std::vector<int>(1,5)); // Create vector with 1 item of value 5
However, I need to stick to an existing interface, so I have to use QList in my case...
@asperamanca From c++11 you can initialize lists, vectors, maps, hashes, etc... using curly brackets:
QHash<int,QList<int>> listHash; listHash.insert(0, {1}); listHash.insert(1, QList<int>{1}); listHash.insert(2, {1, 2, 3, 4}); QHash<int,QList<int>> listHash2 = { {0, {1, 2, 3}}, {1, {4, 5}}, {2, {6, 7, 8}}, };
-
OK, I simplified the problem too much. I do not have a constant value for initialization, but an argument:
void addToHash(const int key, const int value) { if (m_Hash.contains(key)) { m_Hash[key].append(value); } else { m_Hash.insert(key,QList<int>{value}); } }
Here, the initializer list causes a compiler error:
'initializing': cannot convert from 'initializer list' to 'QList<int>'EDIT:
And here I circumvent my original problem:void addToHash(const int key, const int value) { m_Hash[key].append(value); }
...since operator[] default-constructs an entry if none exists.