clear [C++ Reference]
Keywords: stl,vector,clear
Date: 2009-09-07T23:00:51-0700

[[clear]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## 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

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •