Results 1 to 10 of 10

Thread: Passing 2d array as parameter

  1. #1
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Passing 2d array as parameter

    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...

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Passing 2d array as parameter

    You could pass somethin like:
    Qt Code:
    1. QVector<QStringList> 2dStringArr;
    To copy to clipboard, switch view to plain text mode 

    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
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Passing 2d array as parameter

    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?):

    Qt Code:
    1. void show(int **tab2D,int howManyRows,int howManyCols)
    2. {
    3. for (int i=0;i<howManyRows;i++)
    4. {
    5. for (int j=0;j<howManyCols;j++)
    6. cout<<tab2D[i][j]<<" ";
    7. cout<<endl;;
    8. }
    9. }
    10.  
    11. int main(int argc, char* argv[])
    12. {
    13. int **tab;
    14. int k,w ;
    15.  
    16. cin>>w;
    17. cin>>k;
    18.  
    19. tab=new
    20. int * [w];
    21. for (int i=0;i<w;i++)
    22. tab[i]=new int[k];
    23.  
    24. for (int i=0;i<w;i++)
    25. for (int j=0;j<k;j++)
    26. tab[i][j]=random(100);
    27.  
    28. show(tab,w,k);
    29.  
    30. for (int i=0;i<w;i++)
    31. delete [] tab[i];
    32. delete [] tab;
    33.  
    34. return 0;
    35. }
    To copy to clipboard, switch view to plain text mode 

    best regards
    Tomasz

  4. #4
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing 2d array as parameter

    Thanks for answers, but now i need to pass back a bunch of widgets, how do i do that?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Passing 2d array as parameter

    Well, the same way.
    Use QVector, its the same principal.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing 2d array as parameter

    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?

  7. #7
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Passing 2d array as parameter

    No QVector<QString> but QVector<QStringList>:

    Qt Code:
    1. QVector<QStringList> vector(10);
    2. vector[0].append("aaa");
    3. vector[0].append("bbb");
    To copy to clipboard, switch view to plain text mode 

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

    best regards
    Tomasz

  8. The following user says thank you to Tomasz for this useful post:

    Archa4 (7th February 2011)

  9. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Passing 2d array as parameter

    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:
    Qt Code:
    1. void my2DFunc(const QVector<QStringList> &my2DArray)
    2. {
    3. //lets iterate:
    4. for(int i=0; my2DArray.size(); i++){
    5. QStringList strList = my2DArray.at(i);
    6. //Do something with the string list.
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 7th February 2011 at 14:43. Reason: typo
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. The following user says thank you to high_flyer for this useful post:

    Archa4 (7th February 2011)

  11. #9
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing 2d array as parameter

    What does
    Quote Originally Posted by Tomasz View Post
    (10)
    stand for?
    is this the height of the array? if yes, then what's up with width?

  12. #10
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Passing 2d array as parameter

    Quote Originally Posted by Archa4 View Post
    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

Similar Threads

  1. Replies: 4
    Last Post: 2nd April 2010, 10:04
  2. Replies: 4
    Last Post: 12th August 2008, 01:55
  3. Replies: 3
    Last Post: 17th July 2008, 07:43
  4. Passing Ui files as parameter
    By hgedek in forum Newbie
    Replies: 3
    Last Post: 12th December 2007, 12:14
  5. Passing QFile objecrt as parameter
    By db in forum Newbie
    Replies: 2
    Last Post: 28th August 2007, 16:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.