How to insert different arrays into a QVector.
-
I have two arrays
example:
a[3]={1,2,3};
b[5]={4,5,6,7,8};i want to insert into QVector . Later i need to retrieve each element of respective arrays from the Qvector. I am getting the size of the vector as 2 but I am unable to retrieve each element of array. Any help will be most appreciated.
-
Hi @Shankar-B
and welcome,can you share some more code, with what you already did, and what you actually want :)
-
How do you insert the values into your QVector? With
push_back(val)
?If you need array "a" and "b" in one container as such, try a 2D-Vector or Vector of Vectors.
You can access them by using
QVector< QVector<int> > vec; // fill vector // [...] // Access elements for(int i = 0; i < vec.size(); i++) { for(int j = 0; j = vec.at(i).size(); j++) { qDebug() << vec.at(i).at(j); } }
-
int a[3]={1,2,3};
int b[5]={4,5,6,7,8};QVector<int> vec; vec.push_back(a[3]); vec.push_back(b[5]); int s = vec.size(); for (int i=0;i<s;i++) { qDebug() << vec[i][i]; }
Since am new to this i am not getting how to access each element of the corresponding array when they merged in to vector. Above lines of code i was trying these thing.
-
vec[i][i];
Use of
i
for both indexes is wrong! And will indeed only print out first 2 elements of the arrays in the 2-element vector.You want more like (untested)
for (int i=0;i<s;i++) for (int j = 0; j < vec[i].size(); j++) { qDebug() << vec[i][j]; }
Further:
vec.push_back(a[3]); vec.push_back(b[5]);
This is only pushing one element from each
a
/b
array into the vector. I think you're wanting to push whole arrays, likevec.push_back(a)
? In which case declarationQVector<int> vec
will be wrong....? -
int a[3]={1,2,3};
int b[5]={4,5,6,7,8};QVector< QVector<int> > vec; vec.push_back(a); vec.push_back(b); for (int i=0;i< vec.size();i++) { for(int j = 0; j = vec.at(i).size(); j++) { qDebug() << vec.at(i).at(j); } } }
when ever am compiling these thing am getting below error.
D:\Qt Projects\Testingexample\mainwindow.cpp:41: error: no matching function for call to 'QVector<QVector<int> >::push_back(int [3])'
vec.push_back(a); -
@Shankar-B
well, yes, you're mixing and matching at will.int a[3]={1,2,3}; != QVector<int>
I would suggest not to use pure c arrays, use the vector container for it. If you need to pass the raw array around later on, you can get that by calling
data()
of the containerQVector<int> a ={1,2,3}; QVector<int> b ={4,5,6,7,8}; QVector<QVector<int> > vec; vec.push_back(a); vec.push_back(b); int s = vec.size(); for (int i=0;i<s;i++) { auto element = vec.at(i); for(int j(0); j < element.size(); j++) qDebug() << i << element[j]; qDebug () << endl; }
-
Because you have a matrix-like vector now, you need to change
push_back(x)
to something like:QVector< QVector<int> > vec; // static indices not a clean solution but just to demontrate vec.at(0).push_back(i); vec.at(1).push_back(j);
But you need to interate through your input arrays first.
-
@Shankar-B great don't forget to use the topic tools to set the topic to solved :)
-
@Shankar-B
You raised a dedicated topic https://forum.qt.io/topic/107950/how-to-convert-const-qvector-quint32-to-vector-int for this question. That's where it belongs, not as a post in the topic too.