Thread: Vector::clear() won't clear

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

    Vector::clear() won't clear

    After I cleared the first vector and copy the second in it there still some characters left behind after copying.
    How is that possible?

    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    int main()
    {
            string str = "Hello";
            vector<char> v(11, '8');
            vector<char> v2;
            for(int i = 0; i < 5; ++i) v2.push_back(str[i]);
    
            v.clear();
            v = v2;
    
            cout << "v: " << &v.at(0)<< "\n\n";
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    because after
    Code:
    v = v2;
    v holds the contents of v2

    Kurt
    EDIT:
    clear doesn't have to destroy the content of the vector it just sets its size to 0. So the content might still be there. The following assignement overwrites only part of that old content.
    Last edited by ZuK; 06-16-2013 at 02:44 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why are you trying to print the contents of the vector as if it were a null terminated string? For example, you might write this instead:
    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    #include <iterator>
    using namespace std;
     
    int main()
    {
        string str = "Hello";
        vector<char> v(11, '8');
        vector<char> v2;
        for(int i = 0; i < 5; ++i) v2.push_back(str[i]);
    
        v.clear();
        v = v2;
    
        copy(v.begin(), v.end(), ostream_iterator<char>(cout, ""));
        cout << endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ok, thanks, I understand. So there is no need to remove the content.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector.clear is creating problem
    By maven in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2013, 03:30 PM
  2. clear int m?
    By mikeyp in forum C++ Programming
    Replies: 9
    Last Post: 03-13-2010, 12:39 AM
  3. is there easy way to clear nested vector?
    By timchen in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2007, 10:18 PM
  4. Custom Vector template: Want to create erase/clear function
    By Bird Killer in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:37 AM
  5. Using delete to clear a STL vector
    By starkhorn in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2005, 02:59 AM