PDA

View Full Version : Passing 2d array as parameter



Archa4
7th February 2011, 11:51
I have to pass a 2d array of Strings to a function in another class.
How should I do it? If u cannot help me with code, then at least point to the articles i should read to learn how to achieve this...

high_flyer
7th February 2011, 12:04
You could pass somethin like:

QVector<QStringList> 2dStringArr;


If u cannot help me with code, then at least point to the articles i should read to learn how to achieve this...
http://lmgtfy.com/?q=C%2B%2B+2d+array+as+argument

Tomasz
7th February 2011, 12:06
Simple code - You've got here 2D array of int, I think it looks similary with strings (maybe You should use QString and QStringList instead strings?):



void show(int **tab2D,int howManyRows,int howManyCols)
{
for (int i=0;i<howManyRows;i++)
{
for (int j=0;j<howManyCols;j++)
cout<<tab2D[i][j]<<" ";
cout<<endl;;
}
}

int main(int argc, char* argv[])
{
int **tab;
int k,w ;

cin>>w;
cin>>k;

tab=new
int * [w];
for (int i=0;i<w;i++)
tab[i]=new int[k];

for (int i=0;i<w;i++)
for (int j=0;j<k;j++)
tab[i][j]=random(100);

show(tab,w,k);

for (int i=0;i<w;i++)
delete [] tab[i];
delete [] tab;

return 0;
}


best regards
Tomasz

Archa4
7th February 2011, 12:23
Thanks for answers, but now i need to pass back a bunch of widgets, how do i do that?

high_flyer
7th February 2011, 12:53
Well, the same way.
Use QVector, its the same principal.

Archa4
7th February 2011, 14:17
Thanks for your answers, but could u, please, explain what should i do with that QVector<QString> 2dStringArray?
Could u, please, tell me how to call the function, and what parameter should be in that function to represent this QVector?

Tomasz
7th February 2011, 14:29
No QVector<QString> but QVector<QStringList>:



QVector<QStringList> vector(10);
vector[0].append("aaa");
vector[0].append("bbb");


In vector[0][1] You have "bbb". The same thing You can do with other rows (vector[0..9]).

best regards
Tomasz

high_flyer
7th February 2011, 14:31
This thread slowly is getting off topic, since your questions are going in the direction of basic C/C++ concepts and syntax.
I will answer this but please be sure to understand basic C/C++ syntax and concepts before asking further questions.

explain what should i do with that QVector<QString> 2dStringArray?
I suggested QVector<QStringList>, and not QVector<QString>
Be sure to read both classes descriptions to understand the difference.

This is your 2D array of strings.
Have a look at the QVector docs to understand what it is doing.
This is your first dimension, its a "list" of "string lists".
QStringList is a list of strings.
By having a vector of QStrigLists you have a 2D array of strings.

A method declaration that takes this array could look something like:


void my2DFunc(const QVector<QStringList> &my2DArray)
{
//lets iterate:
for(int i=0; my2DArray.size(); i++){
QStringList strList = my2DArray.at(i);
//Do something with the string list.
}
}

Archa4
7th February 2011, 14:32
What does

(10)
stand for?
is this the height of the array? if yes, then what's up with width?

Tomasz
7th February 2011, 14:36
What does stand for?
is this the height of the array? if yes, then what's up with width?

It's number of rows. Columns You add by append() function of QStringList (read class reference for more information about other useful functions). Of course You can write it in Your own way, that was only example.

best regards
Tomasz