Results 1 to 13 of 13

Thread: <vector>

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

    Default <vector>

    Hi,
    I'm looking for resize a vector without destroy it. (eg. I want push_back 10 elements in my vector without use push_back(object), because I don't want create an instance of object)
    Is it possible?
    Thanks
    Regards

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

    Default Re: <vector>

    std::vector can't have empty spaces. Even when you resize it, it will be populated with objects.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    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>

    You must use a vector or list of pointers if you want to avoid creating empty objects.

  4. #4
    Join Date
    Jan 2006
    Location
    Russia
    Posts
    16
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: <vector>

    Isn't the 'vector::reserve()' what you are looking for?

    Qt Code:
    1. struct Obj { };
    2. std::vector<Obj> objs;
    3. objs.reserve(10);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    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>

    "reserve" creates empty objects. Vectora can't have placeholders to objects without actual objects.

    Looks like, although it should, reserve actually does not create objects. However it may be implementation dependent.
    Last edited by wysota; 8th January 2006 at 18:27.

  6. #6
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: <vector>

    Easy. Just do a resize. For example
    Qt Code:
    1. std::vector< double > foo(100, 1.234567);
    To copy to clipboard, switch view to plain text mode 
    creates a vector of 100 doubles.

    Now resize the vector to 200 elements but do not destroy or modify the current elements. You can choose to initialise the new elements or to leave them alone:
    Qt Code:
    1. foo.resize(200); // Resizes to 200 elements.
    2. foo.resize(300, 4.56789); // Resizes from 200 to 300 elements and initialises only the new elements to 4.56789
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    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>

    I just got funny results with experimenting with resize()

    Qt Code:
    1. #include <vector>
    2. #include <iostream>
    3.  
    4. class Test{
    5. public:
    6. Test(){ static int _no = 1;
    7. std::cout << "Constructed object no " << _no++ << std::endl;
    8. }
    9. };
    10.  
    11. int main(){
    12. std::vector<Test> x;
    13. x.resize(10);
    14. x.resize(15);
    15. x.resize(20);
    16. x[ 15 ] = Test();
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Result:
    $ ./a.out
    Constructed object no 1
    Constructed object no 2
    Constructed object no 3
    Constructed object no 4
    I understand constructing of the last test object (because of calling the constructor directly), but what about the first three? It looks like each resize() call creates one object of contained type. Can anyone explain it?
    Last edited by wysota; 8th January 2006 at 21:21.

  8. #8
    Join Date
    Jan 2006
    Location
    Russia
    Posts
    16
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: <vector>

    seems you forget about copy ctors.

    You are right, the behaviour of vector is implementation defined. That's the gcc 3.4.2 output:

    ctor 0
    copy ctor 1
    copy ctor 2
    copy ctor 3
    dtor 4
    ctor 5
    copy opt 6
    dtor 7
    dtor 8
    dtor 9
    dtor 10
    Qt Code:
    1. #include <vector>
    2. #include <iostream>
    3.  
    4. class Test
    5. {
    6. static int _no;
    7. public:
    8. Test()
    9. {
    10. std::cout << "ctor " << _no++ << std::endl;
    11. }
    12. Test(const Test&)
    13. {
    14. std::cout << "copy ctor " << _no++ << std::endl;
    15. }
    16. ~Test()
    17. {
    18. std::cout << "dtor " << _no++ << std::endl;
    19. }
    20. Test& operator= (const Test&)
    21. {
    22. std::cout << "copy opt " << _no++ << std::endl;
    23. }
    24. };
    25.  
    26. int Test::_no = 0;
    27.  
    28. int main()
    29. {
    30. std::vector<Test> x;
    31. x.resize(3);
    32.  
    33. x[ 15 ] = Test();
    34. return 0;
    35. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: <vector>

    http://www.cppreference.com/cppvector/resize.html and http://www.cppreference.com/containers.html
    Qt Code:
    1. void resize( size_type num, const TYPE& val = TYPE() );
    To copy to clipboard, switch view to plain text mode 
    When describing the functions associated with these various containers, this website defines the word TYPE to be the object type that the container holds.
    See what happens when you don't give a second argument?
    Last edited by yop; 8th January 2006 at 21:48.

  10. #10
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: <vector>

    If you ignore second argument, it receives the value that function TYPE() returns.
    Qt 5.3 Opensource & Creator 3.1.2

  11. #11
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: <vector>

    Though my question was retorical , you re right

  12. #12
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: <vector>

    LOL, why do you post retorical questions?
    Qt 5.3 Opensource & Creator 3.1.2

  13. #13
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: <vector>

    I'm Greek, we invented them

Similar Threads

  1. runtime error <vector>
    By mickey in forum General Programming
    Replies: 10
    Last Post: 5th September 2006, 13:18
  2. <vector> and new
    By mickey in forum General Programming
    Replies: 11
    Last Post: 18th May 2006, 16:27

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.