is there easy way to clear nested vector?

This is a discussion on is there easy way to clear nested vector? within the C++ Programming forums, part of the General Programming Boards category; Hi, I use nested vectors a lot and I am wondering is there a easy way to clear such data ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    1

    is there easy way to clear nested vector?

    Hi,

    I use nested vectors a lot and I am wondering is there a easy way to clear such data structure than use many nested for loop to clear each vectors?

    For example, assume I am going to release a nested vector v, I have to write

    vector<vector<int > > v;

    for(int i=0;i<v.size();i++)
    v[i].clear();
    v.clear();

    and it becomes quite ugly when I have 3D or 4D vectors... Is there any easier way to clear the vector(especially when the elements in vector are pointers) ?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    1) I think that if you do a simple
    Code:
    v.clear();
    then any storage associated with the former elements of v is available for reuse.

    2) The memory used by a vector is associated with its capacity, not its size, and clear() only sets the size to 0. If you want to free up memory used, use the "swap trick" as described in

    http://www.informit.com/guides/conte...eqNum=268&rl=1

    3) If v is local to a scope, then any resources associated with v are freed automatically when v goes out of scope, so in this case you don't have to do anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector updation
    By dpp in forum C++ Programming
    Replies: 8
    Last Post: 05-18-2009, 04:25 PM
  2. Sorting vector via recursive functions
    By porsche911nfs in forum C++ Programming
    Replies: 18
    Last Post: 05-04-2009, 06:54 AM
  3. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  4. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 09:14 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21