C++ vector::clear() Function



The C++ vector::clear() function is used to remove every element from a vector and is defined in the <vector> library. When the vector is empty, its size is 0. Vectors are useful data structures that function like dynamic one-dimensional arrays.The time complexity of the clear() function is linear.

Calling this function does not guarantee that a reallocation will take place or that the vector capacity will change. It invalidates any iterators, references, or pointers referring to the contained elements. Any past-the-end iterators is also revoked. The clear() function has linear complexity that scales with the size of the container.

Syntax

Following is the syntax for C++ vector::clear() Function −

void clear() noexcept;

Parameters

It doesn't contains any kind of parameters.

Example 1

Let's consider the following example, where we are going to use the clear() function and retrieving the output which print total elements in vector before using clear() and after using clear().

#include <iostream>
#include <vector>
using namespace std;

int main(){
   vector<int> tutorial = {12,23,34,45,56,67};
   unsigned int vectorsize = tutorial.size();
   cout << "Before clear() :" << vectorsize << endl;
   tutorial.clear();
   vectorsize = tutorial.size();
   cout << "After clear(): " << vectorsize << endl;
   cout << endl;
   return 0;
}

Output

When we compile and run the above program, this will produce the following result −

Before clear() :6
After clear(): 0

Example 2

In the following example, we are going to use the clear() function and observing that the capacity of the vector remains same after the use of clear().

#include <iostream>
#include <algorithm>
#include <vector>

int main(){
   std::vector<int> tutorial{11,22,33,44,55};
   auto print = [](const int& n) {
      std::cout << " " << n;
   };
   std::cout << "Before clear:";
   std::for_each(tutorial.begin(), tutorial.end(), print);
   std::cout << "\nSize=" << tutorial.size() << ", Capacity=" << tutorial.capacity() << '\n';
   std::cout << "Clear\n";
   tutorial.clear();
   std::cout << "After clear:";
   std::for_each(tutorial.begin(), tutorial.end(), print);
   std::cout << "\nSize=" << tutorial.size() << ", Capacity=" << tutorial.capacity() << '\n';
}

Output

On running the above program, it will produce the following result −

Before clear: 11 22 33 44 55
Size=5, Capacity=5
Clear
After clear:
Size=0, Capacity=5

Example 3

Considering the following example, which throws an error as we declared vector with constant that makes the clear() function to throw error.

#include <vector>
#include <iostream>
using namespace std;

int main(){
   const vector<char> x = {'W', 'E', 'L', 'C', '0', 'M','E'};
   for (vector<char>::const_iterator it = x.begin(); it != x.end(); it++)
      cout << *it << ' ';
   cout << endl;
   x.clear();
   for (vector<char>::const_iterator it = x.begin(); it != x.end(); it++)
      cout << *it << ' ';
   cout << endl;
   return 0;
}

Output

On running the above program, it will produce the following result −

main.cpp:10:12: error: passing 'const std::vector<char>
Advertisements