Thread: vector resizing

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, thanks.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by dwks
    Yes, but my vector isn't resizing at all. I get a segmentation fault!
    This:
    Code:
    v[x]
    calls the operator[]() function. Like an array, a vector indexes its elements, and if the index doesn't exist then the operator[]() function fails.

    Vectors grow automatically in size when you add elements to the vector. There are several functions defined to allow you to add elements to a vector. However, operator[]() does not add elements to the vector--it tries to access elements that already exist. In a map<>, operator[]() does add elements to the map, but it doesn't work the same way with a vector.

    If you want to add elements to a vector, then you have to use functions that do that.

Popular pages Recent additions subscribe to a feed