Results 1 to 6 of 6

Thread: vector of vector size

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default vector of vector size

    hi, I need to retrieve this (but i wouldn't like use 'i'):
    Qt Code:
    1. vector < vector <double> >* e
    2. e->at(i).size()
    3. e->at(0).size() //it's the same
    To copy to clipboard, switch view to plain text mode 
    In this case the vector contains n vectors which must have the same size!
    so e->at(0).size() and e->at(1).size() and e->at(n).size() are the same value;
    But I don't like use (0) the reach the size.

    How can I do?
    and: maybe it isn't necessary use a vector of vector !?

    thanks
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector of vector size

    All depends what you need... Why do you keep a pointer to the vector instead of referencing it by value?

    Anyway:
    Qt Code:
    1. double val = (*e)[i][0];
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: vector of vector size

    Quote Originally Posted by wysota View Post
    Why do you keep a pointer to the vector instead of referencing it by value?
    Qt Code:
    1. Net::Net(vector < vector <double> >* e)
    To copy to clipboard, switch view to plain text mode 
    if I refer it in the function by value, isn't create a copy inside that memeber? Inside it I only retrieve the values of 'e', so I think pointer or value is the same!? (but pointer avoid me create an unuseful copy of 'e' !??!).
    thanks
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector of vector size

    You can always pass a reference to avoid making copies.

    Qt Code:
    1. void someFunc(const vector<int> &myvec){
    2. // no copy here
    3. printf("VECTOR HAS %d ELEMENTS\n", myvec.size());
    4. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: vector of vector size

    yes, Does 'pointer' and 'reference' reach the same goal? (in this case). is there one strong reason to use 'reference' in this case?
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector of vector size

    Quote Originally Posted by mickey View Post
    yes, Does 'pointer' and 'reference' reach the same goal? (in this case).
    In this case yes.

    is there one strong reason to use 'reference' in this case?
    References are easier to handle and you can't have "dangling references" in contrast to "dangling pointers" (meaning you wouldn't get the segmentation fault you got). Some people even say that Using Pointers In C++ Is Bad (C).

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.