clear();

This is a discussion on clear(); within the C++ Programming forums, part of the General Programming Boards category; If you have 3 different kind of vectors like this: Code: std::vector<std::vector<string> > Word2(100, std::vector<string>(100)); std::vector<string> Word1(100); std::vector<double> Number1(100); Am ...

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

    clear();

    If you have 3 different kind of vectors like this:

    Code:
    std::vector<std::vector<string> > Word2(100, std::vector<string>(100));
    std::vector<string> Word1(100);
    std::vector<double> Number1(100);
    Am I using the right method to clear All contents in these by doing like this:

    Code:
    Word2.clear();
    Word1.clear();
    Number1.clear();

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Word2 is a vector of vectors. By clearing it that way, you are emptying it of it's dimensions. Thisis valid, but might not be what you want to do if you want it to always be 100x100.

    In fact, in all three cases, are you trying to empty the contents, or reset the values to empty (or 0)? Calling clear removes all the elements, so no indexes are valid and the container is empty. If you want the containers to still contain elements, but make those elements empty, then I would use something like this:
    Code:
    Word1.swap(std::vector<string>(Word1.size()));

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes this is what I was after, just to Reset all contents to 0 and for example keep 100x100.

    I wonder 2 things though.

    When using like above:
    Code:
    (Word1.size())
    Does size meen the size that was given, and to reset these, in this case 100 ?

    If doing that to the Word2 wich is a 2D vector how would that look like

    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Word1.size() will be 100 in that case. If you did push_back, insert, erase or something else to the vector to change the size then it would be different. It's best to use Word1.size() anyway, so if you ever change the size of Word1 then you don't have to change that line of code.

    For Word2, it is the same idea. Take the declaration of Word2, but remove the variable name. Then put that inside the call to Word2.swap(). Instead of 100, you can use Word2.size() and Word2[0].size() to make sure that the sizes of the dimension don't change. Word2[0].size() is potentially dangerous if Word2 is empty, so it's up to you to decide whether it is worth the risk.

    Note also that using swap here might be a premature optimization, since you can just use assignment as well:
    Code:
    Word1 = std::vector<string>(Word1.size());
    In fact, the swap version might not even be an optimization at all. Use whichever is clearer to you.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Does size meen the size that was given, and to reset these, in this case 100 ?

    If doing that to the Word2 wich is a 2D vector how would that look like

    Thanks
    Actually, the only thing that this code does is swap out the old vector, word1, with a new one of the same size that you just created.

    Code:
    std::vector<std::string> word1();
    // ... use word1
    word1.swap(std::string>(word1.size()));
    I imagine a vector of vectors is similar in that you may want to keep its dimensions:
    Code:
    typedef std::vector<std::string> string_vector;
    
    std::vector<string_vector> word2;
    // ... use word 2
    word2.swap(std::vector<string_vector>(word2.size()));
    More info here:
    http://www.cppreference.com/cppvector/swap.html
    Last edited by whiteflags; 02-28-2008 at 04:33 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> word2.swap(std::vector<string_vector>(word2.size() ));
    That's not quite right. You have to specify the size of the second dimension.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thanks, yes that made it much more clearer...

    I think I understand now what is happening.
    I will experiment with these.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About clear(). Please help!
    By Antigloss in forum C++ Programming
    Replies: 12
    Last Post: 07-14-2005, 04:02 AM
  2. MFC: Clear Client Area
    By mrafcho001 in forum Windows Programming
    Replies: 2
    Last Post: 06-27-2005, 01:35 PM
  3. How to Clear in Visual C++
    By MyDestiny in forum Windows Programming
    Replies: 4
    Last Post: 03-16-2005, 09:40 PM
  4. How to Clear in Visual C++ 6.0
    By MyDestiny in forum Windows Programming
    Replies: 1
    Last Post: 03-16-2005, 10:57 AM
  5. Using a button to clear a list box!
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2002, 07:44 PM

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