Results 1 to 4 of 4

Thread: question on vector an new

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

    Default question on vector an new

    hi, I need to obtain a vector of arrays;
    Is this correct? thanks
    Qt Code:
    1. GLfloat* position = new GLfloat[4];
    2. vector <GLfloat*> ppp;
    3. for (int g=0; g < ....; g++) {
    4. for (int i=0; i<4; i++) {
    5. position[i] = ch.toInt(); // split a string as "1543"
    6. }
    7.  
    8. ppp.push_back(position);
    9. }
    10.  
    11. for( std::vector<GLfloat*>::iterator i = ppp.begin(); i != ppp.end(); ++i ) {
    12. delete [] *i;
    13. }
    14. ppp.clear();
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: question on vector an new

    No, because you create and change only one array. In result ppp contains a number of pointers to that single array.

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

    Default Re: question on vector an new

    Right?
    Qt Code:
    1. vector <GLfloat*> ppp;
    2. for (int g=0; g < ....; g++) {
    3. GLfloat* position = new GLfloat[4];
    4. for (int i=0; i<4; i++) {
    5. position[i] = ch.toInt(); // split a string as "1543"
    6. }
    7. ppp.push_back(position);
    8. }
    9.  
    10. for( std::vector<GLfloat*>::iterator i = ppp.begin(); i != ppp.end(); ++i ) {
    11. delete [] *i;
    12. }
    13. ppp.clear();
    To copy to clipboard, switch view to plain text mode 
    and how obtain value of fist element array of first element vector?? thanks
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: question on vector an new

    Quote Originally Posted by mickey
    Right?
    Now it looks OK.

    Quote Originally Posted by mickey
    and how obtain value of fist element array of first element vector?? thanks
    Qt Code:
    1. GLfloat f = ppp[0][0];
    2. // or
    3. GLfloat f = ppp.at( 0 )[ 0 ];
    To copy to clipboard, switch view to plain text mode 
    http://www.cppreference.com/cppvector/at.html

Similar Threads

  1. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  2. Beginner C++ question
    By masoroso in forum General Programming
    Replies: 2
    Last Post: 19th April 2006, 14:15
  3. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09
  4. QTextEdit Qt4: simple question
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 12:03
  5. xml with binary question
    By TheKedge in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2006, 23:21

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.