clear

Syntax:

    #include <vector>
    void clear();

The function clear() deletes all of the elements in the vector. This will call the destructor for all of the elements in the vector.

After a call to clear, the vector's new size will be zero. The vector's capacity however, will not be changed, and the vector will not release its allocated memory.

If you want to empty a vector of all of its elements, as well as its capacity, then you can use the swap trick:

    std::vector aVector;
    [...]
    aVector.swap( std::vector() );

This will create a new temporary empty vector, which will swap with the vector you wish to empty

clear() runs in linear time.

Related Topics: erase swap